-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.ts
378 lines (344 loc) · 9.74 KB
/
LinkedList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { compare } from "./util";
// Be shared with all list data struct
export class Node<T> {
public data: T;
public next: any;
public prev: any;
constructor(d: T) {
this.data = d;
this.next = null;
this.prev = null;
}
}
// single link list
export class List<T> {
public head: Node<T>;
public listSize: number;
constructor() {
this.head = new Node(null); // empty head
this.listSize = 0;
}
// find one Node with data elem, if not found ,return null
public find(elem: T): Node<T> {
if (elem == null) {
return null;
}
let node = this.head.next;
while (node) {
if (compare(node.data, elem)) {
return node;
}
node = node.next;
}
return null;
}
// insert a new element after elem,
// if elem ommits, the new element will insert to the tail by default
public insert(newElem: T, elem?: T) {
if (elem == undefined) {
this.insertTail(newElem);
return;
}
let newNode = new Node<T>(newElem);
let findNode = this.find(elem);
if (findNode == null) { // insert to tail
if (this.head.next == null) { // this first node
this.head.next = newNode;
} else {
let lastNode = this.findLastNode();
lastNode.next = newNode;
}
} else { // insert after the found node
newNode.next = findNode.next;
findNode.next = newNode;
}
this.listSize++;
}
// find the last node
public findLastNode(): Node<T> {
let node = this.head.next;
if (node == null) {
return this.head;
} else {
let pre = null;
while (node) {
pre = node;
node = node.next;
}
return pre;
}
}
// insert elem to the list tail
public insertTail(elem: T) {
let node = new Node(elem);
let lastNode = this.findLastNode();
lastNode.next = node;
this.listSize++;
}
// remove an element , if success ,return true,eles false
public remove(elem: T): boolean {
let pre = this.head;
let cur = pre.next;
while (cur != null) {
if (compare(cur.data, elem)) { // find the node
pre.next = cur.next;
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
cur = cur.next;
}
return false;
}
// advance n node from current node
// if curr is null, the from the head advance n node
// else from the current node
// if n great the distance between currnode and the tail node,
// then return the tail node ,else return Nth node after the curr.
public advance(n: number, curr?: Node<T>): Node<T> {
if (this.listSize == 0) {
return null;
}
let cnt = 0;
curr = this.head.next;
while (curr) {
cnt++;
if (curr.next == null) {
return curr;
}
curr = curr.next;
if (cnt == n) {
return curr;
}
}
return null;
}
public count(): number {
return this.listSize;
}
// travse the list
public display() {
let node = this.head.next;
console.log("display the list: ")
while (node) {
console.log(node.data);
node = node.next;
}
}
}
// double linked list
export class DLList<T> {
private head: Node<T>;
private listSize: number;
constructor() {
this.head = new Node<T>(null);
this.listSize = 0;
}
public find(elem: T): Node<T> {
if (elem == null) {
return null;
}
let node = this.head.next;
while (node) {
if (compare(node.data, elem)) {
return node;
}
node = node.next;
}
return null;
}
public insert(newElem: T, elem?: T) {
if (elem == undefined) {
this.insertTail(newElem);
return;
}
let newNode = new Node<T>(newElem);
let findNode = this.find(elem);
if (findNode == null) { // insert to tail
if (this.head.next == null) { // this first node
this.head.next = newNode;
newNode.prev = this.head;
} else {
let lastNode = this.findLastNode();
lastNode.next = newNode;
newNode.prev = lastNode;
}
} else { // insert after the found node
if (findNode.next == null) {
findNode.next = newNode;
newNode.prev = findNode;
} else {
newNode.next = findNode.next;
findNode.next.prev = newNode;
findNode.next = newNode;
newNode.prev = findNode;
}
}
this.listSize++;
}
public findLastNode(): Node<T> {
let node = this.head.next;
if (node == null) {
return this.head;
} else {
let pre = null;
while (node) {
pre = node;
node = node.next;
}
return pre;
}
}
public insertTail(elem: T) {
let node = new Node(elem);
let lastNode = this.findLastNode();
lastNode.next = node;
node.prev = lastNode;
this.listSize++;
}
public remove(elem: T): boolean {
let pre = this.head;
let cur = pre.next;
while (cur != null) {
if (compare(cur.data, elem)) { // find the node
if (cur.next == null) { // cur is the last node
pre.next = null;
} else {
pre.next = cur.next;
cur.next.prev = pre;
}
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
cur = cur.next;
}
return false;
}
// back n node from current node
// if curr is null, then return the head node
// else from the current node back to head
// if n great the distance between currnode and the head node,
// then return the head node ,else return Nth node before the curr.
public back(n: number, curr?: Node<T>): Node<T> {
if (this.listSize == 0 || curr == undefined) {
return this.head;
}
let cnt = 0;
while (curr) {
cnt++;
if (curr.prev == null) {
return curr;
}
curr = curr.prev;
if (cnt == n) {
return curr;
}
}
return null;
}
public count(): number {
return this.listSize;
}
public display() {
console.log("display the list: ");
let node = this.head.next;
while (node) {
console.log(node.data);
node = node.next;
}
}
public dispReverse() {
console.log("reverse display the list: ");
let last = this.findLastNode();
while (last.prev != null) {
console.log(last.data);
last = last.prev;
}
}
}
// circle linked list
export class CLList<T> {
private head: Node<T>;
private listSize: number;
constructor() {
this.head = new Node(null); // empty head
this.head.next = this.head;
this.listSize = 0;
}
public find(elem: T): Node<T> {
if (elem == null) {
return null;
}
let node = this.head.next;
while (node != this.head) {
if (compare(node.data, elem)) {
return node;
}
node = node.next;
}
return null;
}
public insert(newElem: T, elem?: T) {
if (elem == undefined) {
this.insertTail(newElem);
return;
}
let newNode = new Node<T>(newElem);
let findNode = this.find(elem);
if (findNode == null) { // insert to tail
if (this.head.next == this.head) { // this first node
this.head.next = newNode;
} else {
let lastNode = this.findLastNode();
lastNode.next = newNode;
}
newNode.next = this.head;
} else { // insert after the found node
newNode.next = findNode.next;
findNode.next = newNode;
}
this.listSize++;
}
public findLastNode(): Node<T> {
let pre = this.head;
let node = this.head.next;
while (node != this.head) {
pre = node;
node = node.next;
}
return pre;
}
public insertTail(elem: T) {
let node = new Node(elem);
let lastNode = this.findLastNode();
node.next = this.head;
lastNode.next = node;
this.listSize++;
}
public remove(elem: T): boolean {
let pre = this.head;
let cur = pre.next;
while (cur != this.head) {
if (compare(cur.data, elem)) { // find the node
pre.next = cur.next;
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
cur = cur.next;
}
return false;
}
public display() {
console.log("display the circle list: ");
let node = this.head.next;
while (node != this.head) {
console.log(node.data);
node = node.next;
}
}
}