/
singlyLinkedList.bundle.js
199 lines (199 loc) · 5.63 KB
/
singlyLinkedList.bundle.js
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
class SinglyNode {
data;
next;
constructor(data1){
this.data = data1;
this.next = null;
}
}
function* addGenerator(currentNode, data1, position) {
let count = 0;
let prevNode = null;
while(currentNode !== null){
if (count === position) {
prevNode.next = new SinglyNode(data1);
prevNode.next.next = currentNode;
yield true;
}
prevNode = currentNode;
currentNode = currentNode.next;
count++;
}
return false;
}
function* updateGenerator(key, currentNode, newValue) {
while(currentNode !== null){
if (currentNode.data.key === key) {
currentNode.data.value = newValue;
yield currentNode.data;
}
currentNode = currentNode.next;
}
return false;
}
function* searchGenerator(key, currentNode) {
while(currentNode !== null){
if (currentNode.data.key === key) {
yield currentNode.data;
}
currentNode = currentNode.next;
}
return false;
}
function* iteratorGenerator(currentNode) {
while(currentNode !== null){
yield currentNode.data;
currentNode = currentNode.next;
}
return false;
}
class SinglyLinkedList1 {
#head;
#tail;
size;
constructor(){
this.#head = null;
this.#tail = null;
this.size = 0;
}
prepend(data) {
if (this.#head === null) {
const newNode = new SinglyNode(data);
this.#head = newNode;
this.#tail = newNode;
return true;
}
const currentNode = this.#head;
this.#head = new SinglyNode(data);
this.#head.next = currentNode;
if (currentNode.next === null) {
this.#tail = currentNode;
}
this.size++;
return true;
}
append(data) {
const newNode = new SinglyNode(data);
if (this.#head === null) {
this.#head = newNode;
this.#tail = newNode;
return true;
}
this.#tail.next = newNode;
this.#tail = newNode;
this.size++;
return false;
}
add(data, position) {
if (position === 0) {
console.error("Use `prepend()` method to insert data at Head.");
return false;
} else if (position === this.size) {
console.error("Use `append()` method to insert data at Tail.");
return false;
} else if (position > this.size && position != this.size + 1) {
console.error(`Out of range. Size of the linkedList is ${this.size}`);
return false;
}
let currentNode = this.#head;
const iterator = addGenerator(currentNode, data, position);
const iteratorNext = iterator.next();
if (iteratorNext.value) {
this.size++;
return true;
}
return false;
}
getFromHead() {
if (this.#head === null) {
return false;
}
return this.#head.data;
}
getFromTail() {
if (this.#tail === null) {
return false;
}
return this.#tail.data;
}
log() {
let currentNode = this.#head;
while(currentNode !== null){
console.log(currentNode.data);
currentNode = currentNode.next;
}
}
remove(key) {
if (this.#head === null) {
return false;
} else if (this.#head.next === null) {
this.#tail = null;
}
if (this.#head.data.key === key) {
const prevHeadData = this.#head.data;
this.#head = this.#head.next;
if (this.#head.next === null) this.#tail = this.#head;
this.size--;
if (this.#head === null) this.size = 0;
return true;
}
let previousNode = null;
let currentNode = this.#head;
while(currentNode !== null){
if (key === currentNode.data.key) {
const temp = currentNode.data;
previousNode.next = currentNode.next;
if (currentNode.next === null) this.#tail = previousNode;
this.size--;
return true;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
return false;
}
update(key, newValue) {
if (this.#head === null) {
return false;
}
if (this.#head.data.key === key) {
this.#head.data.value = newValue;
return this.#head.data;
}
if (this.#tail.data.key === key) {
this.#tail.data.value = newValue;
return this.#tail.data;
}
let currentNode = this.#head.next;
const iterator = updateGenerator(key, currentNode, newValue);
const iteratorNext = iterator.next();
if (iteratorNext.value) {
return iteratorNext.value;
}
return false;
}
search(key) {
if (this.#head === null) {
return false;
}
if (this.#head.data.key === key) {
return this.#head.data;
}
if (this.#tail.data.key === key) {
return this.#tail.data;
}
let currentNode = this.#head.next;
const iterator = searchGenerator(key, currentNode);
const iteratorNext = iterator.next();
if (iteratorNext.value) {
return iteratorNext.value;
}
return false;
}
iterator() {
const currentNode = this.#head;
const iterator = iteratorGenerator(currentNode);
return iterator;
}
}
export { SinglyLinkedList1 as SinglyLinkedList };