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