|
4 | 4 | * sake of simplicity.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -export function DoublyLinkedList(){ |
| 7 | +export function DoublyLinkedList() { |
8 | 8 | this.front = new Node(null, null, null);
|
9 | 9 | this.back = new Node(this.front, null, null);
|
10 | 10 | this.front.next = this.back;
|
11 | 11 | this.length = 0;
|
12 | 12 | }
|
13 | 13 |
|
14 |
| -export function Node(prev, next, value){ |
| 14 | +export function Node(prev, next, value) { |
15 | 15 | this.prev = prev;
|
16 | 16 | this.next = next;
|
17 | 17 | this.value = value;
|
18 | 18 | }
|
19 | 19 |
|
20 |
| -export function Iterator(front, back, current){ |
| 20 | +export function Iterator(front, back, current) { |
21 | 21 | this.front = front;
|
22 | 22 | this.back = back;
|
23 | 23 | this.current = current;
|
24 | 24 | }
|
25 | 25 |
|
26 |
| -export function ReverseIterator(front, back, current){ |
| 26 | +export function ReverseIterator(front, back, current) { |
27 | 27 | this.front = front;
|
28 | 28 | this.back = back;
|
29 | 29 | this.current = current;
|
30 | 30 | }
|
31 | 31 |
|
32 |
| -DoublyLinkedList.prototype.insertAfter = function(iterator, value){ |
33 |
| - var node, prev; |
| 32 | +DoublyLinkedList.prototype.insertAfter = function (iterator, value) { |
| 33 | + const prev = iterator.current; |
34 | 34 |
|
35 |
| - prev = iterator.current; |
36 |
| - |
37 |
| - node = new Node(prev, prev.next, value); |
| 35 | + const node = new Node(prev, prev.next, value); |
38 | 36 | prev.next.prev = node;
|
39 | 37 | prev.next = node;
|
40 | 38 |
|
41 | 39 | ++this.length;
|
42 | 40 | return this.iterator(node);
|
43 |
| -} |
44 |
| - |
45 |
| -DoublyLinkedList.prototype.insertBefore = function(iterator, value){ |
46 |
| - var node, next; |
| 41 | +}; |
47 | 42 |
|
48 |
| - next = iterator.current; |
| 43 | +DoublyLinkedList.prototype.insertBefore = function (iterator, value) { |
| 44 | + const next = iterator.current; |
49 | 45 |
|
50 |
| - node = new Node(next.prev, next, value); |
| 46 | + const node = new Node(next.prev, next, value); |
51 | 47 | next.prev.next = node;
|
52 | 48 | next.prev = node;
|
53 | 49 |
|
54 | 50 | ++this.length;
|
55 | 51 | return this.iterator(node);
|
56 |
| -} |
| 52 | +}; |
57 | 53 |
|
58 |
| -DoublyLinkedList.prototype.unshift = function(value){ |
| 54 | +DoublyLinkedList.prototype.unshift = function (value) { |
59 | 55 | return this.insertAfter(this.begin(), value);
|
60 |
| -} |
| 56 | +}; |
61 | 57 |
|
62 |
| -DoublyLinkedList.prototype.push = function(value){ |
| 58 | +DoublyLinkedList.prototype.push = function (value) { |
63 | 59 | return this.insertBefore(this.end(), value);
|
64 |
| -} |
| 60 | +}; |
65 | 61 |
|
66 |
| -DoublyLinkedList.prototype.erase = function(iterator){ |
67 |
| - var node = iterator.current; |
| 62 | +DoublyLinkedList.prototype.erase = function (iterator) { |
| 63 | + const node = iterator.current; |
68 | 64 |
|
69 | 65 | node.prev.next = node.next;
|
70 | 66 | node.next.prev = node.prev;
|
71 | 67 |
|
72 | 68 | --this.length;
|
73 | 69 | return this.iterator(node.next);
|
74 |
| -} |
| 70 | +}; |
75 | 71 |
|
76 |
| -DoublyLinkedList.prototype.rerase = function(iterator){ |
77 |
| - var node = iterator.current; |
| 72 | +DoublyLinkedList.prototype.rerase = function (iterator) { |
| 73 | + const node = iterator.current; |
78 | 74 |
|
79 | 75 | node.next.prev = node.prev;
|
80 | 76 | node.prev.next = node.next;
|
81 | 77 |
|
82 | 78 | --this.length;
|
83 | 79 | return this.iterator(node.prev);
|
84 |
| -} |
| 80 | +}; |
85 | 81 |
|
86 |
| -DoublyLinkedList.prototype.eraserange = function(first, last){ |
87 |
| - var firstnode, lastnode, it; |
88 |
| - firstnode = first.current; |
89 |
| - lastnode = last.current; |
| 82 | +DoublyLinkedList.prototype.eraserange = function (first, last) { |
| 83 | + const firstnode = first.current; |
| 84 | + const lastnode = last.current; |
90 | 85 |
|
91 | 86 | lastnode.prev = firstnode.prev;
|
92 | 87 | firstnode.prev.next = lastnode;
|
93 | 88 |
|
94 |
| - it = first.copy(); |
| 89 | + const it = first.copy(); |
95 | 90 |
|
96 | 91 | while (it.current !== lastnode) {
|
97 | 92 | --this.length;
|
98 | 93 | it.next();
|
99 | 94 | }
|
| 95 | + |
100 | 96 | return last.copy();
|
101 |
| -} |
| 97 | +}; |
102 | 98 |
|
103 |
| -DoublyLinkedList.prototype.reraserange = function(first, last){ |
104 |
| - var firstnode, lastnode, it; |
105 |
| - firstnode = first.current; |
106 |
| - lastnode = last.current; |
| 99 | +DoublyLinkedList.prototype.reraserange = function (first, last) { |
| 100 | + const firstnode = first.current; |
| 101 | + const lastnode = last.current; |
107 | 102 |
|
108 | 103 | lastnode.next = firstnode.next;
|
109 | 104 | firstnode.next.prev = lastnode;
|
110 | 105 |
|
111 |
| - it = first.copy(); |
| 106 | + const it = first.copy(); |
112 | 107 |
|
113 | 108 | while (it.current !== lastnode) {
|
114 | 109 | --this.length;
|
115 | 110 | it.next();
|
116 | 111 | }
|
117 |
| - return last.copy(); |
118 |
| -} |
119 |
| - |
120 |
| -DoublyLinkedList.prototype.shift = function ( ) { |
121 | 112 |
|
122 |
| - if ( this.length === 0 ) return null ; |
123 |
| - |
124 |
| - const node = this.front.next ; |
| 113 | + return last.copy(); |
| 114 | +}; |
125 | 115 |
|
126 |
| - this.front.next = node.next ; |
127 |
| - node.next.prev = this.front ; |
| 116 | +DoublyLinkedList.prototype.shift = function () { |
| 117 | + if (this.length === 0) return null; |
128 | 118 |
|
129 |
| - --this.length ; |
| 119 | + const node = this.front.next; |
130 | 120 |
|
131 |
| - return node.value ; |
| 121 | + this.front.next = node.next; |
| 122 | + node.next.prev = this.front; |
132 | 123 |
|
133 |
| -} |
| 124 | + --this.length; |
134 | 125 |
|
135 |
| -DoublyLinkedList.prototype.pop = function ( ) { |
| 126 | + return node.value; |
| 127 | +}; |
136 | 128 |
|
137 |
| - if ( this.length === 0 ) return null ; |
| 129 | +DoublyLinkedList.prototype.pop = function () { |
| 130 | + if (this.length === 0) return null; |
138 | 131 |
|
139 |
| - const node = this.back.prev ; |
| 132 | + const node = this.back.prev; |
140 | 133 |
|
141 |
| - this.back.prev = node.prev ; |
142 |
| - node.prev.next = this.back ; |
| 134 | + this.back.prev = node.prev; |
| 135 | + node.prev.next = this.back; |
143 | 136 |
|
144 |
| - --this.length ; |
| 137 | + --this.length; |
145 | 138 |
|
146 |
| - return node.value ; |
| 139 | + return node.value; |
| 140 | +}; |
147 | 141 |
|
148 |
| -} |
149 |
| - |
150 |
| -DoublyLinkedList.prototype.clear = function(){ |
| 142 | +DoublyLinkedList.prototype.clear = function () { |
151 | 143 | this.front.next = this.back;
|
152 | 144 | this.back.prev = this.front;
|
153 | 145 | this.length = 0;
|
154 | 146 | return this;
|
155 |
| -} |
| 147 | +}; |
156 | 148 |
|
157 |
| -DoublyLinkedList.prototype.iterator = function(node){ |
| 149 | +DoublyLinkedList.prototype.iterator = function (node) { |
158 | 150 | return new Iterator(this.front, this.back, node);
|
159 |
| -} |
| 151 | +}; |
160 | 152 |
|
161 |
| -DoublyLinkedList.prototype.riterator = function(node){ |
| 153 | +DoublyLinkedList.prototype.riterator = function (node) { |
162 | 154 | return new ReverseIterator(this.front, this.back, node);
|
163 |
| -} |
| 155 | +}; |
164 | 156 |
|
165 |
| -DoublyLinkedList.prototype.begin = function(){ |
| 157 | +DoublyLinkedList.prototype.begin = function () { |
166 | 158 | return this.iterator(this.front);
|
167 |
| -} |
| 159 | +}; |
168 | 160 |
|
169 |
| -DoublyLinkedList.prototype.end = function(){ |
| 161 | +DoublyLinkedList.prototype.end = function () { |
170 | 162 | return this.iterator(this.back);
|
171 |
| -} |
| 163 | +}; |
172 | 164 |
|
173 |
| -DoublyLinkedList.prototype.rbegin = function(){ |
| 165 | +DoublyLinkedList.prototype.rbegin = function () { |
174 | 166 | return this.riterator(this.back);
|
175 |
| -} |
| 167 | +}; |
176 | 168 |
|
177 |
| -DoublyLinkedList.prototype.rend = function(){ |
| 169 | +DoublyLinkedList.prototype.rend = function () { |
178 | 170 | return this.riterator(this.front);
|
179 |
| -} |
| 171 | +}; |
180 | 172 |
|
181 |
| -Iterator.prototype.copy = function() { |
| 173 | +Iterator.prototype.copy = function () { |
182 | 174 | return new Iterator(this.front, this.back, this.current);
|
183 |
| -} |
| 175 | +}; |
184 | 176 |
|
185 |
| -ReverseIterator.prototype.copy = function() { |
| 177 | +ReverseIterator.prototype.copy = function () { |
186 | 178 | return new ReverseIterator(this.front, this.back, this.current);
|
187 |
| -} |
188 |
| - |
189 |
| -Iterator.prototype.next = |
190 |
| -ReverseIterator.prototype.prev = function ( ) { |
191 |
| - |
192 |
| - const c = this.current = this.current.next ; |
| 179 | +}; |
193 | 180 |
|
194 |
| - return c === this.back ? { done : true } : { value : c.value , done : false } ; |
195 |
| - |
196 |
| -} |
| 181 | +// eslint-disable-next-line no-multi-assign |
| 182 | +Iterator.prototype.next = ReverseIterator.prototype.prev = function () { |
| 183 | + // eslint-disable-next-line no-multi-assign |
| 184 | + const c = (this.current = this.current.next); |
197 | 185 |
|
198 |
| -Iterator.prototype.prev = |
199 |
| -ReverseIterator.prototype.next = function ( ) { |
| 186 | + return c === this.back ? {done: true} : {value: c.value, done: false}; |
| 187 | +}; |
200 | 188 |
|
201 |
| - const c = this.current = this.current.prev ; |
| 189 | +// eslint-disable-next-line no-multi-assign |
| 190 | +Iterator.prototype.prev = ReverseIterator.prototype.next = function () { |
| 191 | + // eslint-disable-next-line no-multi-assign |
| 192 | + const c = (this.current = this.current.prev); |
202 | 193 |
|
203 |
| - return c === this.front ? { done : true } : { value : c.value , done : false } ; |
| 194 | + return c === this.front ? {done: true} : {value: c.value, done: false}; |
| 195 | +}; |
204 | 196 |
|
205 |
| -} |
206 |
| - |
207 |
| -DoublyLinkedList.prototype[Symbol.iterator] = DoublyLinkedList.prototype.begin ; |
| 197 | +DoublyLinkedList.prototype[Symbol.iterator] = DoublyLinkedList.prototype.begin; |
208 | 198 | DoublyLinkedList.Node = Node;
|
209 | 199 | DoublyLinkedList.Iterator = Iterator;
|
210 | 200 | DoublyLinkedList.ReverseIterator = ReverseIterator;
|
211 |
| - |
212 |
| - |
0 commit comments