-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-double.js
More file actions
142 lines (126 loc) · 3.97 KB
/
Copy path6-double.js
File metadata and controls
142 lines (126 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
class DoubleList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
#node(index) {
if (index < this.length / 2) {
let current = this.head;
let i = 0;
while (i < index) {
current = current.next;
i++;
}
return current;
}
let current = this.tail;
let i = this.length - 1;
while (i > index) {
current = current.prev;
i--;
}
return current;
}
prepend(value) {
const node = { value, prev: null, next: this.head };
if (this.head === null) this.tail = node;
else this.head.prev = node;
this.head = node;
this.length++;
return this;
}
// Better than singly linked: O(1) via tail, no walk to the end.
//
// head tail
// │ │
// ▼ ▼
// ┌───┐ ◀──▶ ┌───┐ ◀──▶ ┌───┐ ◀──▶ ┌───┐
// │ 3 │ │ 2 │ │ 1 │ │ 0 │ ← append
// └───┘ └───┘ └───┘ └───┘
append(value) {
const node = { value, prev: this.tail, next: null };
if (this.tail === null) this.head = node;
else this.tail.next = node;
this.tail = node;
this.length++;
return this;
}
// Better than singly linked: start from the nearer end (head or tail).
insert(index, value) {
if (index <= 0) return this.prepend(value);
if (index >= this.length) return this.append(value);
const next = this.#node(index);
const prev = next.prev;
const node = { value, prev, next };
prev.next = node;
next.prev = node;
this.length++;
return this;
}
// Better than singly linked: unlink with prev/next; last node via tail.
//
// … ◀──▶ ┌───┐ ◀──▶ ┌───┐ ◀──▶ ┌───┐ ◀──▶ …
// │ A │ │ X │ │ B │
// └───┘ └───┘ └───┘
// │ │
// └─────────▶◀───────────┘ delete X
delete(index) {
if (this.head === null) throw new Error('List is empty');
if (index < 0 || index >= this.length) {
throw new Error('Index out of bounds');
}
const node = this.#node(index);
if (node.prev === null) this.head = node.next;
else node.prev.next = node.next;
if (node.next === null) this.tail = node.prev;
else node.next.prev = node.prev;
this.length--;
return this;
}
// Better than singly linked: start from the nearer end (head or tail).
at(index) {
if (index < 0 || index >= this.length) {
throw new Error('Index out of bounds');
}
return this.#node(index).value;
}
toArray() {
const result = new Array(this.length);
let current = this.head;
let i = 0;
while (current !== null) {
result[i++] = current.value;
current = current.next;
}
return result;
}
*[Symbol.iterator]() {
let current = this.head;
while (current !== null) {
yield current.value;
current = current.next;
}
}
}
// Usage
const list = new DoubleList();
list.prepend(1);
list.prepend(2);
list.prepend(3);
// list ──▶ DoubleList { length: 3 }
// │ │
// ▼ head ▼ tail
// ┌───┐ ◀──▶ ┌───┐ ◀──▶ ┌───┐
// │ 3 │ │ 2 │ │ 1 │
// └───┘ └───┘ └───┘
console.log(`list.toArray() ->`, list.toArray());
console.log(`list.at(1) ->`, list.at(1));
console.log(`[...list] ->`, [...list]);
list.append(0);
console.log(`after append(0) ->`, list.toArray());
list.insert(1, 4);
console.log(`after insert(1, 4) ->`, list.toArray());
list.delete(2);
console.log(`after delete(2) ->`, list.toArray());