-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateLinkedList.ts
172 lines (135 loc) · 3.36 KB
/
createLinkedList.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
import { LinkedListNode } from "./createLinkedListNode.js";
class LinkedList {
head: any;
tail: any;
comparator: (() => any) | ((i: any, j: any) => number);
constructor(comparator?: () => any) {
this.head = null;
this.tail = null;
this.comparator =
comparator ||
function (i, j): number {
if (i < j) return -1;
if (i > j) return 1;
return 0;
};
}
prepend(item: any) {
let newNode = new LinkedListNode(item, this.head);
this.head = newNode;
if (!this.tail) {
this.tail = newNode;
}
return newNode;
}
append(item: any) {
let newNode = new LinkedListNode(item);
if (this.tail) {
this.tail.next = newNode;
}
this.tail = newNode;
if (!this.head) {
this.head = newNode;
}
return newNode;
}
delete(item: any) {
if (!this.head) {
return null;
}
let deleteNode = null;
while (this.head && this.comparator(this.head.value, item) === 0) {
deleteNode = this.head;
this.head = this.head.next;
}
let current = this.head;
if (current !== null) {
while (current.next) {
if (this.comparator(current.next.value, item) === 0) {
deleteNode = current.next;
current.next = current.next.next;
} else {
current = current.next;
}
}
}
if (this.comparator(this.tail.value, item)) {
this.tail = current;
}
return deleteNode;
}
deleteHead() {
if (!this.head) {
return null;
}
let deletedHead = this.head;
if (this.head.next) {
this.head = this.head.next;
} else {
this.head = null;
this.tail = null;
}
return deletedHead;
}
deleteTail() {
const deletedTail = this.tail;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
return deletedTail;
}
let current = this.head;
while (current.next) {
if (!current.next.next) {
current.next = null;
} else {
current = current.next;
}
}
this.tail = current;
return deletedTail;
}
forEach(fn_cb: (node: any) => void) {
let current = this.head;
while (current) {
fn_cb(current);
current = current.next;
}
}
reverseForEach(fn_cb: (node: any) => void) {
function tick(node: { value: any; next: any }) {
if (node) {
tick(node.next);
fn_cb(node);
}
}
tick(this.head);
}
find(item: any) {
if (!this.head) {
return null;
}
let current = this.head;
while (current) {
if (this.comparator(current.value, item) === 0) {
return current;
}
current = current.next;
}
return null;
}
}
const l_list = new LinkedList();
console.log("prepend value 5:", l_list.prepend(5));
console.log("prepend value 12:", l_list.prepend(12));
console.log("append value 6:", l_list.append(6));
console.log("append value 7:", l_list.append(7));
console.log("delete value 12:", l_list.delete(12));
console.log("append value 4:", l_list.append(4));
console.log("delete head:", l_list.deleteHead());
console.log("delete tail:", l_list.deleteTail());
l_list.forEach((node) => console.log("doubled:", node.value * 2));
l_list.reverseForEach((node) => console.log("half:", node.value / 2));
console.log("find 6", l_list.find(6));
console.log(l_list);
export { LinkedList };