-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly-linked_list.js
More file actions
143 lines (115 loc) · 3.1 KB
/
Copy pathdoubly-linked_list.js
File metadata and controls
143 lines (115 loc) · 3.1 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
// create a node:
class Node {
constructor(value) {
this.previous = null;
this.value = value;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// insert node at last from the linkedlist:
insertLast(value) {
this.length++;
const newNode = new Node(value);
if (this.tail) {
// list is not empty
this.tail.next = newNode;
newNode.previous = this.tail;
this.tail = newNode;
return newNode;
}
// list is empty
this.head = this.tail = newNode;
return newNode;
}
// insert node at first from the linkedlist:
insertFirst(value) {
this.length++;
const newNode = new Node(value);
if (this.head) {
this.head.previous = newNode;
newNode.next = this.head;
this.head = newNode;
return newNode;
}
this.head = this.tail = newNode;
return newNode;
}
// remove node at last from the linkedlist:
removeLast() {
if (this.tail) {
this.length--;
const removedTail = this.tail;
this.tail = this.tail.previous;
if (this.tail) this.tail.next = null;
else this.head = null;
return removedTail;
}
return undefined;
}
// remove node at last from the linkedlist:
removeFirst() {
if (this.head) {
this.length--;
const removeHead = this.head;
this.head = this.head.next;
if (this.head) this.head.previous = null;
else this.tail = null;
return removeHead;
}
return undefined;
}
// insert at specific index:
insertIndex(value, index) {
if (index >= this.length) throw new Error('Insert index out of bounds!');
if (index === 0) return this.insertFirst(value);
this.length++;
let currentNode = this.head;
for (let i = 0; i < index; i++) currentNode = currentNode.next;
const previousNode = currentNode.previous;
const newNode = new Node(value);
newNode.next = currentNode;
newNode.previous = previousNode;
previousNode.next = newNode;
currentNode.previous = newNode;
return newNode;
}
// remove at specific index:
removeIndex(index) {
if (index >= this.length)
throw new Error('Remove index out of bounds!');
if (index === 0)
return this.removeFirst();
this.length--;
let currentNode = this.head;
for (let i = 0; i < index; i++)
currentNode = currentNode.next;
const previousNode = currentNode.previous;
const nextNode = currentNode.next;
previousNode.next = nextNode;
nextNode.previous = previousNode;
return currentNode;
}
// print the LinkedList:
printDoublyLinkedList() {
let current = this.head;
while (current) {
// console.log(current.previous?.value, current.value, current.next?.value);
console.log(current.value);
current = current.next;
}
}
}
const instance = new DoublyLinkedList();
instance.insertLast('7');
instance.insertLast('8');
instance.insertLast('9');
instance.insertFirst('6');
instance.removeLast();
instance.removeFirst();
// instance.printDoublyLinkedList();