-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked Lists.js
194 lines (171 loc) · 4.12 KB
/
Linked Lists.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
// 10 --> 5 --> 16
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class SinglyLinkedList {
constructor(value) {
this.head = {
value,
next: null,
};
this.tail = this.head;
}
append(value) {
const newNode = new Node(value);
// Changing the pointer of the current tail
this.tail.next = newNode;
// Setting the new tail
this.tail = newNode;
this.length++;
return this;
}
prepend(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
printList() {
const output = [];
let currentNode = this.head;
while (currentNode) {
output.push(currentNode.value);
currentNode = currentNode.next;
}
return output;
}
insert(index, value) {
const newNode = new Node(value);
const leader = this.traverseToIndex(index - 1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.next = holdingPointer;
this.length++;
return this.printList();
}
delete(index) {
const leader = this.traverseToIndex(index - 1);
const node = leader.next;
const holdingPointer = node.next;
leader.next = holdingPointer;
this.length--;
return this.printList();
}
traverseToIndex(index) {
const values = this.printList();
let currentNode = this.head;
while (currentNode.value !== values[index]) {
currentNode = currentNode.next;
}
return currentNode;
}
}
class DoublyLinkedList {
constructor(value) {
this.head = {
value,
next: null,
prev: null,
};
this.tail = this.head;
}
append(value) {
const newNode = new Node(value);
newNode.prev = this.tail;
// Changing the pointer of the current tail
this.tail.next = newNode;
// Setting the new tail
this.tail = newNode;
this.length++;
return this;
}
prepend(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
this.length++;
return this;
}
printList() {
const output = [];
let currentNode = this.head;
while (currentNode) {
output.push(currentNode.value);
currentNode = currentNode.next;
}
return output;
}
insert(index, value) {
const newNode = new Node(value);
const leader = this.traverseToIndex(index - 1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.prev = leader;
newNode.next = holdingPointer;
holdingPointer.prev = newNode;
this.length++;
return this.printList();
}
delete(index) {
const leader = this.traverseToIndex(index - 1);
const node = leader.next;
const holdingPointer = node.next;
leader.next = holdingPointer;
this.length--;
return this.printList();
}
traverseToIndex(index) {
const values = this.printList();
let currentNode = this.head;
while (currentNode.value !== values[index]) {
currentNode = currentNode.next;
}
return currentNode;
}
lookup(index) {
const values = this.printList();
let currentNode = this.head.next;
while (currentNode.value !== values[index]) {
currentNode = currentNode.next;
}
return currentNode;
}
reverze() {
let head = this.head;
let tail = this.tail;
this.head = tail;
this.tail = head;
const values = this.printList();
let currentNode = traverseToIndex(values.length - 1);
// this.head.next =
// while(currentNode.value !== this.head.value) {
// }
}
reverse() {
let index = 0;
const values = this.printList();
const prevHead = this.head;
this.head = prevHead;
const prevTail = this.tail;
while (index < values.length - 1) {
let node = this.traverseToIndex(index);
this.head.next = node;
index++;
}
return this.printList();
}
}
const myLinkedList = new DoublyLinkedList(5);
myLinkedList.append(10);
myLinkedList.append(15);
myLinkedList.append(20);
myLinkedList.insert(1, 20);
myLinkedList.insert(2, 25);
// myLinkedList.printList();
myLinkedList.reverse();