-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly-linked-list-v2.js
More file actions
241 lines (209 loc) · 5.63 KB
/
Copy pathdoubly-linked-list-v2.js
File metadata and controls
241 lines (209 loc) · 5.63 KB
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
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
append(element) {
let node = new Node(element);
if (this.isEmpty()) {
this.head = node;
this.tail = node;
}
else {
let temp = this.tail;
this.tail = node;
temp.next = this.tail
this.tail.prev = temp;
}
this.size++;
}
prepend(element) {
let node = new Node(element);
if (this.isEmpty()) {
this.head = node;
this.tail = node;
}
else {
let temp = this.head;
this.head = node;
node.next = temp;
temp.prev = node;
}
this.size++;
}
insertAt(element, location) {
let node = new Node(element);
if (location < 0 || location > this.sizeOfList()) {
return false;
}
if (this.isEmpty() && location != 0) {
return false;
}
if (location === 0) {
this.prepend(element);
}
else if (this.sizeOfList() === location) {
this.append(element);
}
else {
let i = 0;
let current = this.head;
while (current) {
if (i === location) {
let next = current.next;
current.next = node;
node.prev = current;
node.next = next;
next.prev = node;
this.size++;
break;
}
else {
current = current.next;
}
i++;
}
}
}
removeElement(element) {
if (this.head.value === element) {
this.head = this.head.next;
this.head.prev = null;
this.size--;
return true;
}
else if (this.tail.value === element) {
this.tail = this.tail.prev;
this.tail.next = null;
this.size--;
return true;
}
else {
let current = this.head;
while (current) {
if (current.value === element) {
let next = current.next;
let prev = current.prev;
prev.next = next;
next.prev = prev;
this.size--;
return true;
}
else {
current = current.next;
}
}
return false;
}
}
removeFrom(location) {
if (this.sizeOfList() < location || location < 0) {
return false;
}
if (location === 0) {
this.head = this.head.next;
this.head.prev = null;
this.size--;
return true;
}
else if (this.sizeOfList() - 1 === location) {
this.tail = this.tail.prev;
this.tail.next = null;
this.size--;
return true;
}
let i = 0;
let current = this.head;
while (current) {
if (i === location) {
let next = current.next;
let prev = current.prev;
prev.next = next;
next.prev = prev;
this.size--;
return true;
}
else {
current = current.next;
}
i++;
}
return false;
}
indexOf(element) {
let i = 0;
let current = this.head;
while (current) {
if (current.value === element) {
return i;
}
else {
current = current.next
}
i++;
}
return false;
}
isEmpty() {
return this.sizeOfList() === 0;
}
sizeOfList() {
return this.size;
}
printList() {
if (this.isEmpty()) {
return 'List is empty'
}
let root = this.head;
let str = '';
while (root.next) {
str += root.value + ' <-> ';
root = root.next;
}
str += root.value;
return str;
}
}
let dll = new DoublyLinkedList();
console.log('Insert 1 at location 0');
dll.insertAt(1, 0);
console.log('Print the list : ', dll.printList());
console.log('Insert 6');
dll.append(6);
console.log('Print the list : ', dll.printList());
console.log('Insert 10');
dll.append(10);
console.log('Print the list : ', dll.printList());
console.log('Prepend 0');
dll.prepend(0);
console.log('Print the list : ', dll.printList());
console.log('Insert 15');
dll.append(15);
console.log('Print the list : ', dll.printList());
console.log('Insert 20 at location 5');
dll.insertAt(20, 5);
console.log('Print the list : ', dll.printList());
console.log('Insert 12 at location 22');
dll.insertAt(12, 22);
console.log('Print the list : ', dll.printList());
console.log('Remove element 6');
dll.removeElement(6);
console.log('Print the list : ', dll.printList());
console.log('Index of element 10 is : ', dll.indexOf(10));
console.log('Remove element 1');
dll.removeElement(1);
console.log('Print the list : ', dll.printList());
console.log('Remove from location 2');
dll.removeFrom(2);
console.log('Print the list : ', dll.printList());
console.log('Remove from location 0');
dll.removeFrom(0);
console.log('Print the list : ', dll.printList());