UNPKG

7.64 kBJavaScriptView Raw
1export var LinkedList = (function () {
2 function LinkedList() {
3 this.length = 0;
4 this.asArray = [];
5 }
6 LinkedList.prototype.getNode = function (position) {
7 if (this.length === 0 || position < 0 || position >= this.length) {
8 throw new Error('Position is out of the list');
9 }
10 var current = this.head;
11 for (var index = 0; index < position; index++) {
12 current = current.next;
13 }
14 return current;
15 };
16 LinkedList.prototype.createInternalArrayRepresentation = function () {
17 var outArray = [];
18 var current = this.head;
19 while (current) {
20 outArray.push(current.value);
21 current = current.next;
22 }
23 this.asArray = outArray;
24 };
25 LinkedList.prototype.get = function (position) {
26 if (this.length === 0 || position < 0 || position >= this.length) {
27 return void 0;
28 }
29 var current = this.head;
30 for (var index = 0; index < position; index++) {
31 current = current.next;
32 }
33 return current.value;
34 };
35 LinkedList.prototype.add = function (value, position) {
36 if (position === void 0) { position = this.length; }
37 if (position < 0 || position > this.length) {
38 throw new Error('Position is out of the list');
39 }
40 var node = {
41 value: value,
42 next: undefined,
43 previous: undefined
44 };
45 if (this.length === 0) {
46 this.head = node;
47 this.tail = node;
48 this.current = node;
49 }
50 else {
51 if (position === 0) {
52 // first node
53 node.next = this.head;
54 this.head.previous = node;
55 this.head = node;
56 }
57 else if (position === this.length) {
58 // last node
59 this.tail.next = node;
60 node.previous = this.tail;
61 this.tail = node;
62 }
63 else {
64 // node in middle
65 var currentPreviousNode = this.getNode(position - 1);
66 var currentNextNode = currentPreviousNode.next;
67 currentPreviousNode.next = node;
68 currentNextNode.previous = node;
69 node.previous = currentPreviousNode;
70 node.next = currentNextNode;
71 }
72 }
73 this.length++;
74 this.createInternalArrayRepresentation();
75 };
76 LinkedList.prototype.remove = function (position) {
77 if (position === void 0) { position = 0; }
78 if (this.length === 0 || position < 0 || position >= this.length) {
79 throw new Error('Position is out of the list');
80 }
81 if (position === 0) {
82 // first node
83 this.head = this.head.next;
84 if (this.head) {
85 // there is no second node
86 this.head.previous = undefined;
87 }
88 else {
89 // there is no second node
90 this.tail = undefined;
91 }
92 }
93 else if (position === this.length - 1) {
94 // last node
95 this.tail = this.tail.previous;
96 this.tail.next = undefined;
97 }
98 else {
99 // middle node
100 var removedNode = this.getNode(position);
101 removedNode.next.previous = removedNode.previous;
102 removedNode.previous.next = removedNode.next;
103 }
104 this.length--;
105 this.createInternalArrayRepresentation();
106 };
107 LinkedList.prototype.set = function (position, value) {
108 if (this.length === 0 || position < 0 || position >= this.length) {
109 throw new Error('Position is out of the list');
110 }
111 var node = this.getNode(position);
112 node.value = value;
113 this.createInternalArrayRepresentation();
114 };
115 LinkedList.prototype.toArray = function () {
116 return this.asArray;
117 };
118 LinkedList.prototype.findAll = function (fn) {
119 var current = this.head;
120 var result = [];
121 for (var index = 0; index < this.length; index++) {
122 if (fn(current.value, index)) {
123 result.push({ index: index, value: current.value });
124 }
125 current = current.next;
126 }
127 return result;
128 };
129 // Array methods overriding start
130 LinkedList.prototype.push = function () {
131 var _this = this;
132 var args = [];
133 for (var _i = 0; _i < arguments.length; _i++) {
134 args[_i - 0] = arguments[_i];
135 }
136 args.forEach(function (arg) {
137 _this.add(arg);
138 });
139 return this.length;
140 };
141 LinkedList.prototype.pop = function () {
142 if (this.length === 0) {
143 return undefined;
144 }
145 var last = this.tail;
146 this.remove(this.length - 1);
147 return last.value;
148 };
149 LinkedList.prototype.unshift = function () {
150 var _this = this;
151 var args = [];
152 for (var _i = 0; _i < arguments.length; _i++) {
153 args[_i - 0] = arguments[_i];
154 }
155 args.reverse();
156 args.forEach(function (arg) {
157 _this.add(arg, 0);
158 });
159 return this.length;
160 };
161 LinkedList.prototype.shift = function () {
162 if (this.length === 0) {
163 return undefined;
164 }
165 var lastItem = this.head.value;
166 this.remove();
167 return lastItem;
168 };
169 LinkedList.prototype.forEach = function (fn) {
170 var current = this.head;
171 for (var index = 0; index < this.length; index++) {
172 fn(current.value, index);
173 current = current.next;
174 }
175 };
176 LinkedList.prototype.indexOf = function (value) {
177 var current = this.head;
178 var position = 0;
179 for (var index = 0; index < this.length; index++) {
180 if (current.value === value) {
181 position = index;
182 break;
183 }
184 current = current.next;
185 }
186 return position;
187 };
188 LinkedList.prototype.some = function (fn) {
189 var current = this.head;
190 var result = false;
191 while (current && !result) {
192 if (fn(current.value)) {
193 result = true;
194 break;
195 }
196 current = current.next;
197 }
198 return result;
199 };
200 LinkedList.prototype.every = function (fn) {
201 var current = this.head;
202 var result = true;
203 while (current && result) {
204 if (!fn(current.value)) {
205 result = false;
206 }
207 current = current.next;
208 }
209 return result;
210 };
211 LinkedList.prototype.toString = function () {
212 return '[Linked List]';
213 };
214 LinkedList.prototype.find = function (fn) {
215 var current = this.head;
216 var result;
217 for (var index = 0; index < this.length; index++) {
218 if (fn(current.value, index)) {
219 result = current.value;
220 break;
221 }
222 current = current.next;
223 }
224 return result;
225 };
226 LinkedList.prototype.findIndex = function (fn) {
227 var current = this.head;
228 var result;
229 for (var index = 0; index < this.length; index++) {
230 if (fn(current.value, index)) {
231 result = index;
232 break;
233 }
234 current = current.next;
235 }
236 return result;
237 };
238 return LinkedList;
239}());
240//# sourceMappingURL=linked-list.class.js.map
\No newline at end of file