singly-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 +131 -5
- 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/singly-linked-list-typed.js.map +1 -1
- package/dist/umd/singly-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,9 +34,135 @@ yarn add singly-linked-list-typed
|
|
|
34
34
|
|
|
35
35
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
36
36
|
|
|
37
|
+
### basic SinglyLinkedList creation and push operation
|
|
38
|
+
```typescript
|
|
39
|
+
// Create a simple SinglyLinkedList with initial values
|
|
40
|
+
const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
41
|
+
|
|
42
|
+
// Verify the list maintains insertion order
|
|
43
|
+
console.log([...list]); // [1, 2, 3, 4, 5];
|
|
44
|
+
|
|
45
|
+
// Check length
|
|
46
|
+
console.log(list.length); // 5;
|
|
47
|
+
|
|
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];
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### SinglyLinkedList pop and shift operations
|
|
55
|
+
```typescript
|
|
56
|
+
const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
57
|
+
|
|
58
|
+
// Pop removes from the end
|
|
59
|
+
const last = list.pop();
|
|
60
|
+
console.log(last); // 50;
|
|
61
|
+
|
|
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;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### SinglyLinkedList unshift and forward traversal
|
|
72
|
+
```typescript
|
|
73
|
+
const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
74
|
+
|
|
75
|
+
// Unshift adds to the beginning
|
|
76
|
+
list.unshift(10);
|
|
77
|
+
console.log([...list]); // [10, 20, 30, 40];
|
|
78
|
+
|
|
79
|
+
// Access elements (forward traversal only for singly linked)
|
|
80
|
+
const second = list.at(1);
|
|
81
|
+
console.log(second); // 20;
|
|
82
|
+
|
|
83
|
+
// SinglyLinkedList allows forward iteration only
|
|
84
|
+
const elements: number[] = [];
|
|
85
|
+
for (const item of list) {
|
|
86
|
+
elements.push(item);
|
|
87
|
+
}
|
|
88
|
+
console.log(elements); // [10, 20, 30, 40];
|
|
89
|
+
|
|
90
|
+
console.log(list.length); // 4;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### SinglyLinkedList filter and map operations
|
|
94
|
+
```typescript
|
|
95
|
+
const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
96
|
+
|
|
97
|
+
// Filter even numbers
|
|
98
|
+
const filtered = list.filter(value => value % 2 === 0);
|
|
99
|
+
console.log(filtered.length); // 2;
|
|
100
|
+
|
|
101
|
+
// Map to double values
|
|
102
|
+
const doubled = list.map(value => value * 2);
|
|
103
|
+
console.log(doubled.length); // 5;
|
|
104
|
+
|
|
105
|
+
// Use reduce to sum
|
|
106
|
+
const sum = list.reduce((acc, value) => acc + value, 0);
|
|
107
|
+
console.log(sum); // 15;
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### SinglyLinkedList for sequentially processed data stream
|
|
111
|
+
```typescript
|
|
112
|
+
interface LogEntry {
|
|
113
|
+
timestamp: number;
|
|
114
|
+
level: 'INFO' | 'WARN' | 'ERROR';
|
|
115
|
+
message: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// SinglyLinkedList is ideal for sequential processing where you only need forward iteration
|
|
119
|
+
// O(1) insertion/deletion at head, O(n) for tail operations
|
|
120
|
+
const logStream = new SinglyLinkedList<LogEntry>();
|
|
121
|
+
|
|
122
|
+
// Simulate incoming log entries
|
|
123
|
+
const entries: LogEntry[] = [
|
|
124
|
+
{ timestamp: 1000, level: 'INFO', message: 'Server started' },
|
|
125
|
+
{ timestamp: 1100, level: 'WARN', message: 'Memory usage high' },
|
|
126
|
+
{ timestamp: 1200, level: 'ERROR', message: 'Connection failed' },
|
|
127
|
+
{ timestamp: 1300, level: 'INFO', message: 'Connection restored' }
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
// Add entries to the stream
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
logStream.push(entry);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
console.log(logStream.length); // 4;
|
|
136
|
+
|
|
137
|
+
// Process logs sequentially (only forward iteration needed)
|
|
138
|
+
const processedLogs: string[] = [];
|
|
139
|
+
for (const log of logStream) {
|
|
140
|
+
processedLogs.push(`[${log.level}] ${log.message}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log(processedLogs); // [
|
|
144
|
+
// '[INFO] Server started',
|
|
145
|
+
// '[WARN] Memory usage high',
|
|
146
|
+
// '[ERROR] Connection failed',
|
|
147
|
+
// '[INFO] Connection restored'
|
|
148
|
+
// ];
|
|
149
|
+
|
|
150
|
+
// Get first log (O(1) - direct head access)
|
|
151
|
+
const firstLog = logStream.at(0);
|
|
152
|
+
console.log(firstLog?.message); // 'Server started';
|
|
153
|
+
|
|
154
|
+
// Remove oldest log (O(1) operation at head)
|
|
155
|
+
const removed = logStream.shift();
|
|
156
|
+
console.log(removed?.message); // 'Server started';
|
|
157
|
+
console.log(logStream.length); // 3;
|
|
158
|
+
|
|
159
|
+
// Remaining logs still maintain order for sequential processing
|
|
160
|
+
console.log(logStream.length); // 3;
|
|
161
|
+
```
|
|
162
|
+
|
|
37
163
|
### implementation of a basic text editor
|
|
38
164
|
```typescript
|
|
39
|
-
|
|
165
|
+
class TextEditor {
|
|
40
166
|
private content: SinglyLinkedList<string>;
|
|
41
167
|
private cursorIndex: number;
|
|
42
168
|
private undoStack: Stack<{ operation: string; data?: any }>;
|
|
@@ -89,17 +215,17 @@ yarn add singly-linked-list-typed
|
|
|
89
215
|
editor.insert('l');
|
|
90
216
|
editor.insert('l');
|
|
91
217
|
editor.insert('o');
|
|
92
|
-
console.log(editor.getText()); // 'Hello' // Output: "Hello"
|
|
218
|
+
console.log(editor.getText()); // 'Hello'; // Output: "Hello"
|
|
93
219
|
|
|
94
220
|
editor.delete();
|
|
95
|
-
console.log(editor.getText()); // 'Hell' // Output: "Hell"
|
|
221
|
+
console.log(editor.getText()); // 'Hell'; // Output: "Hell"
|
|
96
222
|
|
|
97
223
|
editor.undo();
|
|
98
|
-
console.log(editor.getText()); // 'Hello' // Output: "Hello"
|
|
224
|
+
console.log(editor.getText()); // 'Hello'; // Output: "Hello"
|
|
99
225
|
|
|
100
226
|
editor.moveCursor(1);
|
|
101
227
|
editor.insert('a');
|
|
102
|
-
console.log(editor.getText()); // 'Haello'
|
|
228
|
+
console.log(editor.getText()); // 'Haello';
|
|
103
229
|
```
|
|
104
230
|
|
|
105
231
|
[//]: # (No deletion!!! End of Example Replace Section)
|