doubly-linked-list-typed 2.2.2 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -412
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/doubly-linked-list-typed.js.map +1 -1
- package/dist/umd/doubly-linked-list-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
package/README.md
CHANGED
|
@@ -34,442 +34,108 @@ yarn add doubly-linked-list-typed
|
|
|
34
34
|
|
|
35
35
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
36
36
|
|
|
37
|
-
###
|
|
37
|
+
### basic DoublyLinkedList creation and push operation
|
|
38
38
|
```typescript
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
{ type: 'insert', content: 'second line of text' },
|
|
42
|
-
{ type: 'delete', content: 'delete the first line' }
|
|
43
|
-
];
|
|
44
|
-
const editorHistory = new DoublyLinkedList<{ type: string; content: string }>(actions);
|
|
45
|
-
|
|
46
|
-
console.log(editorHistory.last?.type); // 'delete'
|
|
47
|
-
console.log(editorHistory.pop()?.content); // 'delete the first line'
|
|
48
|
-
console.log(editorHistory.last?.type); // 'insert'
|
|
49
|
-
```
|
|
39
|
+
// Create a simple DoublyLinkedList with initial values
|
|
40
|
+
const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
50
41
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const browserHistory = new DoublyLinkedList<string>();
|
|
42
|
+
// Verify the list maintains insertion order
|
|
43
|
+
console.log([...list]); // [1, 2, 3, 4, 5];
|
|
54
44
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
browserHistory.push('details page');
|
|
45
|
+
// Check length
|
|
46
|
+
console.log(list.length); // 5;
|
|
58
47
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
console.log(
|
|
48
|
+
// Push a new element to the end
|
|
49
|
+
list.push(6);
|
|
50
|
+
console.log(list.length); // 6;
|
|
51
|
+
console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
62
52
|
```
|
|
63
53
|
|
|
64
|
-
###
|
|
54
|
+
### DoublyLinkedList pop and shift operations
|
|
65
55
|
```typescript
|
|
66
|
-
|
|
67
|
-
interface Song {
|
|
68
|
-
title: string;
|
|
69
|
-
artist: string;
|
|
70
|
-
duration: number; // duration in seconds
|
|
71
|
-
}
|
|
56
|
+
const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
72
57
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
constructor(songs: Song[]) {
|
|
78
|
-
this.playlist = new DoublyLinkedList<Song>();
|
|
79
|
-
songs.forEach(song => this.playlist.push(song));
|
|
80
|
-
this.currentSong = this.playlist.head;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Play the next song in the playlist
|
|
84
|
-
playNext(): Song | undefined {
|
|
85
|
-
if (!this.currentSong?.next) {
|
|
86
|
-
this.currentSong = this.playlist.head; // Loop to the first song
|
|
87
|
-
} else {
|
|
88
|
-
this.currentSong = this.currentSong.next;
|
|
89
|
-
}
|
|
90
|
-
return this.currentSong?.value;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Play the previous song in the playlist
|
|
94
|
-
playPrevious(): Song | undefined {
|
|
95
|
-
if (!this.currentSong?.prev) {
|
|
96
|
-
this.currentSong = this.playlist.tail; // Loop to the last song
|
|
97
|
-
} else {
|
|
98
|
-
this.currentSong = this.currentSong.prev;
|
|
99
|
-
}
|
|
100
|
-
return this.currentSong?.value;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Get the current song
|
|
104
|
-
getCurrentSong(): Song | undefined {
|
|
105
|
-
return this.currentSong?.value;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Loop through the playlist twice
|
|
109
|
-
loopThroughPlaylist(): Song[] {
|
|
110
|
-
const playedSongs: Song[] = [];
|
|
111
|
-
const initialNode = this.currentSong;
|
|
112
|
-
|
|
113
|
-
// Loop through the playlist twice
|
|
114
|
-
for (let i = 0; i < this.playlist.length * 2; i++) {
|
|
115
|
-
playedSongs.push(this.currentSong!.value);
|
|
116
|
-
this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Reset the current song to the initial song
|
|
120
|
-
this.currentSong = initialNode;
|
|
121
|
-
return playedSongs;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
58
|
+
// Pop removes from the end
|
|
59
|
+
const last = list.pop();
|
|
60
|
+
console.log(last); // 50;
|
|
124
61
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
];
|
|
131
|
-
|
|
132
|
-
// should play the next song
|
|
133
|
-
player = new Player(songs);
|
|
134
|
-
const firstSong = player.getCurrentSong();
|
|
135
|
-
const nextSong = player.playNext();
|
|
136
|
-
|
|
137
|
-
// Expect the next song to be "Hotel California by Eagles"
|
|
138
|
-
console.log(nextSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
|
|
139
|
-
console.log(firstSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
|
|
140
|
-
|
|
141
|
-
// should play the previous song
|
|
142
|
-
player = new Player(songs);
|
|
143
|
-
player.playNext(); // Move to the second song
|
|
144
|
-
const currentSong = player.getCurrentSong();
|
|
145
|
-
const previousSong = player.playPrevious();
|
|
146
|
-
|
|
147
|
-
// Expect the previous song to be "Bohemian Rhapsody by Queen"
|
|
148
|
-
console.log(previousSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
|
|
149
|
-
console.log(currentSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
|
|
150
|
-
|
|
151
|
-
// should loop to the first song when playing next from the last song
|
|
152
|
-
player = new Player(songs);
|
|
153
|
-
player.playNext(); // Move to the second song
|
|
154
|
-
player.playNext(); // Move to the third song
|
|
155
|
-
player.playNext(); // Move to the fourth song
|
|
156
|
-
|
|
157
|
-
const nextSongToFirst = player.playNext(); // Should loop to the first song
|
|
158
|
-
|
|
159
|
-
// Expect the next song to be "Bohemian Rhapsody by Queen"
|
|
160
|
-
console.log(nextSongToFirst); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
|
|
161
|
-
|
|
162
|
-
// should loop to the last song when playing previous from the first song
|
|
163
|
-
player = new Player(songs);
|
|
164
|
-
player.playNext(); // Move to the first song
|
|
165
|
-
player.playNext(); // Move to the second song
|
|
166
|
-
player.playNext(); // Move to the third song
|
|
167
|
-
player.playNext(); // Move to the fourth song
|
|
168
|
-
|
|
169
|
-
const previousToLast = player.playPrevious(); // Should loop to the last song
|
|
170
|
-
|
|
171
|
-
// Expect the previous song to be "Billie Jean by Michael Jackson"
|
|
172
|
-
console.log(previousToLast); // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
|
|
173
|
-
|
|
174
|
-
// should loop through the entire playlist
|
|
175
|
-
player = new Player(songs);
|
|
176
|
-
const playedSongs = player.loopThroughPlaylist();
|
|
177
|
-
|
|
178
|
-
// The expected order of songs for two loops
|
|
179
|
-
console.log(playedSongs); // [
|
|
180
|
-
// { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
|
|
181
|
-
// { title: 'Hotel California', artist: 'Eagles', duration: 391 },
|
|
182
|
-
// { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
|
|
183
|
-
// { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 },
|
|
184
|
-
// { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
|
|
185
|
-
// { title: 'Hotel California', artist: 'Eagles', duration: 391 },
|
|
186
|
-
// { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
|
|
187
|
-
// { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
|
|
188
|
-
// ]
|
|
62
|
+
// Shift removes from the beginning
|
|
63
|
+
const first = list.shift();
|
|
64
|
+
console.log(first); // 10;
|
|
65
|
+
|
|
66
|
+
// Verify remaining elements
|
|
67
|
+
console.log([...list]); // [20, 30, 40];
|
|
68
|
+
console.log(list.length); // 3;
|
|
189
69
|
```
|
|
190
70
|
|
|
191
|
-
###
|
|
71
|
+
### DoublyLinkedList for...of iteration and map operation
|
|
192
72
|
```typescript
|
|
193
|
-
|
|
194
|
-
key: K;
|
|
195
|
-
value: V;
|
|
196
|
-
}
|
|
73
|
+
const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
197
74
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
private map: Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>;
|
|
202
|
-
|
|
203
|
-
constructor(capacity: number) {
|
|
204
|
-
if (capacity <= 0) {
|
|
205
|
-
throw new Error('lru cache capacity must be greater than 0');
|
|
206
|
-
}
|
|
207
|
-
this.capacity = capacity;
|
|
208
|
-
this.list = new DoublyLinkedList<CacheEntry<K, V>>();
|
|
209
|
-
this.map = new Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Get the current cache length
|
|
213
|
-
get length(): number {
|
|
214
|
-
return this.list.length;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Check if it is empty
|
|
218
|
-
get isEmpty(): boolean {
|
|
219
|
-
return this.list.isEmpty();
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Get cached value
|
|
223
|
-
get(key: K): V | undefined {
|
|
224
|
-
const node = this.map.get(key);
|
|
225
|
-
|
|
226
|
-
if (!node) return undefined;
|
|
227
|
-
|
|
228
|
-
// Move the visited node to the head of the linked list (most recently used)
|
|
229
|
-
this.moveToFront(node);
|
|
230
|
-
|
|
231
|
-
return node.value.value;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Set cache value
|
|
235
|
-
set(key: K, value: V): void {
|
|
236
|
-
// Check if it already exists
|
|
237
|
-
const node = this.map.get(key);
|
|
238
|
-
|
|
239
|
-
if (node) {
|
|
240
|
-
// Update value and move to head
|
|
241
|
-
node.value.value = value;
|
|
242
|
-
this.moveToFront(node);
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Check capacity
|
|
247
|
-
if (this.list.length >= this.capacity) {
|
|
248
|
-
// Delete the least recently used element (the tail of the linked list)
|
|
249
|
-
const removedNode = this.list.tail;
|
|
250
|
-
if (removedNode) {
|
|
251
|
-
this.map.delete(removedNode.value.key);
|
|
252
|
-
this.list.pop();
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Create new node and add to head
|
|
257
|
-
const newEntry: CacheEntry<K, V> = { key, value };
|
|
258
|
-
this.list.unshift(newEntry);
|
|
259
|
-
|
|
260
|
-
// Save node reference in map
|
|
261
|
-
const newNode = this.list.head;
|
|
262
|
-
if (newNode) {
|
|
263
|
-
this.map.set(key, newNode);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Delete specific key
|
|
268
|
-
delete(key: K): boolean {
|
|
269
|
-
const node = this.map.get(key);
|
|
270
|
-
if (!node) return false;
|
|
271
|
-
|
|
272
|
-
// Remove from linked list
|
|
273
|
-
this.list.delete(node);
|
|
274
|
-
// Remove from map
|
|
275
|
-
this.map.delete(key);
|
|
276
|
-
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// Clear cache
|
|
281
|
-
clear(): void {
|
|
282
|
-
this.list.clear();
|
|
283
|
-
this.map.clear();
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// Move the node to the head of the linked list
|
|
287
|
-
private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
|
|
288
|
-
this.list.delete(node);
|
|
289
|
-
this.list.unshift(node.value);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
75
|
+
// Iterate through list
|
|
76
|
+
const doubled = list.map(value => value * 2);
|
|
77
|
+
console.log(doubled.length); // 5;
|
|
292
78
|
|
|
293
|
-
//
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
console.log(cache.get('a')); // 1
|
|
300
|
-
console.log(cache.get('b')); // 2
|
|
301
|
-
console.log(cache.get('c')); // 3
|
|
302
|
-
|
|
303
|
-
// The least recently used element should be evicted when capacity is exceeded
|
|
304
|
-
cache.clear();
|
|
305
|
-
cache.set('a', 1);
|
|
306
|
-
cache.set('b', 2);
|
|
307
|
-
cache.set('c', 3);
|
|
308
|
-
cache.set('d', 4); // This will eliminate 'a'
|
|
309
|
-
|
|
310
|
-
console.log(cache.get('a')); // undefined
|
|
311
|
-
console.log(cache.get('b')); // 2
|
|
312
|
-
console.log(cache.get('c')); // 3
|
|
313
|
-
console.log(cache.get('d')); // 4
|
|
314
|
-
|
|
315
|
-
// The priority of an element should be updated when it is accessed
|
|
316
|
-
cache.clear();
|
|
317
|
-
cache.set('a', 1);
|
|
318
|
-
cache.set('b', 2);
|
|
319
|
-
cache.set('c', 3);
|
|
320
|
-
|
|
321
|
-
cache.get('a'); // access 'a'
|
|
322
|
-
cache.set('d', 4); // This will eliminate 'b'
|
|
323
|
-
|
|
324
|
-
console.log(cache.get('a')); // 1
|
|
325
|
-
console.log(cache.get('b')); // undefined
|
|
326
|
-
console.log(cache.get('c')); // 3
|
|
327
|
-
console.log(cache.get('d')); // 4
|
|
328
|
-
|
|
329
|
-
// Should support updating existing keys
|
|
330
|
-
cache.clear();
|
|
331
|
-
cache.set('a', 1);
|
|
332
|
-
cache.set('a', 10);
|
|
333
|
-
|
|
334
|
-
console.log(cache.get('a')); // 10
|
|
335
|
-
|
|
336
|
-
// Should support deleting specified keys
|
|
337
|
-
cache.clear();
|
|
338
|
-
cache.set('a', 1);
|
|
339
|
-
cache.set('b', 2);
|
|
340
|
-
|
|
341
|
-
console.log(cache.delete('a')); // true
|
|
342
|
-
console.log(cache.get('a')); // undefined
|
|
343
|
-
console.log(cache.length); // 1
|
|
344
|
-
|
|
345
|
-
// Should support clearing cache
|
|
346
|
-
cache.clear();
|
|
347
|
-
cache.set('a', 1);
|
|
348
|
-
cache.set('b', 2);
|
|
349
|
-
cache.clear();
|
|
350
|
-
|
|
351
|
-
console.log(cache.length); // 0
|
|
352
|
-
console.log(cache.isEmpty); // true
|
|
79
|
+
// Use for...of loop
|
|
80
|
+
const result: number[] = [];
|
|
81
|
+
for (const item of list) {
|
|
82
|
+
result.push(item);
|
|
83
|
+
}
|
|
84
|
+
console.log(result); // [1, 2, 3, 4, 5];
|
|
353
85
|
```
|
|
354
86
|
|
|
355
|
-
###
|
|
87
|
+
### Browser history
|
|
356
88
|
```typescript
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
{ time: 16000, text: 'And the tears come streaming down your face' },
|
|
367
|
-
{ time: 20000, text: "When you lose something you can't replace" },
|
|
368
|
-
{ time: 24000, text: 'When you love someone, but it goes to waste' },
|
|
369
|
-
{ time: 28000, text: 'Could it be worse?' },
|
|
370
|
-
{ time: 32000, text: 'Lights will guide you home' },
|
|
371
|
-
{ time: 36000, text: 'And ignite your bones' },
|
|
372
|
-
{ time: 40000, text: 'And I will try to fix you' }
|
|
373
|
-
];
|
|
374
|
-
|
|
375
|
-
// Populate the DoublyLinkedList with lyrics
|
|
376
|
-
lyrics.forEach(lyric => lyricsList.push(lyric));
|
|
377
|
-
|
|
378
|
-
// Test different scenarios of lyric synchronization
|
|
379
|
-
|
|
380
|
-
// 1. Find lyric at exact timestamp
|
|
381
|
-
const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
|
|
382
|
-
console.log(exactTimeLyric?.text); // 'And ignite your bones'
|
|
383
|
-
|
|
384
|
-
// 2. Find lyric between timestamps
|
|
385
|
-
const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
|
|
386
|
-
console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
|
|
387
|
-
|
|
388
|
-
// 3. Find first lyric when timestamp is less than first entry
|
|
389
|
-
const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
|
|
390
|
-
console.log(earlyTimeLyric); // undefined
|
|
391
|
-
|
|
392
|
-
// 4. Find last lyric when timestamp is after last entry
|
|
393
|
-
const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
|
|
394
|
-
console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
89
|
+
const browserHistory = new DoublyLinkedList<string>();
|
|
90
|
+
|
|
91
|
+
browserHistory.push('home page');
|
|
92
|
+
browserHistory.push('search page');
|
|
93
|
+
browserHistory.push('details page');
|
|
94
|
+
|
|
95
|
+
console.log(browserHistory.last); // 'details page';
|
|
96
|
+
console.log(browserHistory.pop()); // 'details page';
|
|
97
|
+
console.log(browserHistory.last); // 'search page';
|
|
395
98
|
```
|
|
396
99
|
|
|
397
|
-
###
|
|
100
|
+
### DoublyLinkedList for LRU cache implementation
|
|
398
101
|
```typescript
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
public priority: number
|
|
403
|
-
) {}
|
|
404
|
-
|
|
405
|
-
execute(): string {
|
|
406
|
-
return `Process ${this.id} executed.`;
|
|
407
|
-
}
|
|
102
|
+
interface CacheEntry {
|
|
103
|
+
key: string;
|
|
104
|
+
value: string;
|
|
408
105
|
}
|
|
409
106
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
executeNext(): string | undefined {
|
|
432
|
-
// Execute tasks at the head of the queue in order
|
|
433
|
-
const process = this.queue.shift();
|
|
434
|
-
return process ? process.execute() : undefined;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
listProcesses(): string[] {
|
|
438
|
-
return this.queue.toArray().map(process => `Process ${process.id} (Priority: ${process.priority})`);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
clear(): void {
|
|
442
|
-
this.queue.clear();
|
|
443
|
-
}
|
|
107
|
+
// Simulate LRU cache using DoublyLinkedList
|
|
108
|
+
// DoublyLinkedList is perfect because:
|
|
109
|
+
// - O(1) delete from any position
|
|
110
|
+
// - O(1) push to end
|
|
111
|
+
// - Bidirectional traversal for LRU policy
|
|
112
|
+
|
|
113
|
+
const cacheList = new DoublyLinkedList<CacheEntry>();
|
|
114
|
+
const maxSize = 3;
|
|
115
|
+
|
|
116
|
+
// Add cache entries
|
|
117
|
+
cacheList.push({ key: 'user:1', value: 'Alice' });
|
|
118
|
+
cacheList.push({ key: 'user:2', value: 'Bob' });
|
|
119
|
+
cacheList.push({ key: 'user:3', value: 'Charlie' });
|
|
120
|
+
|
|
121
|
+
// Try to add a new entry when cache is full
|
|
122
|
+
if (cacheList.length >= maxSize) {
|
|
123
|
+
// Remove the oldest (first) entry
|
|
124
|
+
const evicted = cacheList.shift();
|
|
125
|
+
console.log(evicted?.key); // 'user:1';
|
|
444
126
|
}
|
|
445
127
|
|
|
446
|
-
//
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
console.log(
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
//
|
|
457
|
-
|
|
458
|
-
// should execute the highest priority process
|
|
459
|
-
scheduler = new Scheduler();
|
|
460
|
-
scheduler.addProcess(new Process(1, 10));
|
|
461
|
-
scheduler.addProcess(new Process(2, 20));
|
|
462
|
-
|
|
463
|
-
console.log(scheduler.executeNext()); // 'Process 2 executed.'
|
|
464
|
-
console.log(scheduler.listProcesses()); // ['Process 1 (Priority: 10)']
|
|
465
|
-
|
|
466
|
-
// should clear all processes
|
|
467
|
-
scheduler = new Scheduler();
|
|
468
|
-
scheduler.addProcess(new Process(1, 10));
|
|
469
|
-
scheduler.addProcess(new Process(2, 20));
|
|
470
|
-
|
|
471
|
-
scheduler.clear();
|
|
472
|
-
console.log(scheduler.listProcesses()); // []
|
|
128
|
+
// Add new entry
|
|
129
|
+
cacheList.push({ key: 'user:4', value: 'Diana' });
|
|
130
|
+
|
|
131
|
+
// Verify current cache state
|
|
132
|
+
console.log(cacheList.length); // 3;
|
|
133
|
+
const cachedKeys = [...cacheList].map(entry => entry.key);
|
|
134
|
+
console.log(cachedKeys); // ['user:2', 'user:3', 'user:4'];
|
|
135
|
+
|
|
136
|
+
// Access entry (in real LRU, this would move it to end)
|
|
137
|
+
const foundEntry = [...cacheList].find(entry => entry.key === 'user:2');
|
|
138
|
+
console.log(foundEntry?.value); // 'Bob';
|
|
473
139
|
```
|
|
474
140
|
|
|
475
141
|
[//]: # (No deletion!!! End of Example Replace Section)
|