-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdoubly_linked_list.js
190 lines (155 loc) · 3.88 KB
/
doubly_linked_list.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
class Node {
constructor(val) {
this.val = val
this.prev = null
this.next = null
}
}
class DoublyLinkedList {
constructor() {
this.head = null
this.tail = null
this.length = 0
}
insertAtTail(val) {
const node = new Node(val)
if (!this.head) {
this.head = node
this.tail = node
this.length++
} else {
node.prev = this.tail
this.tail.next = node
this.tail = node
this.length++
}
}
insertAtHead(val) {
const node = new Node(val)
if (!this.head) {
this.head = node
this.tail = node
this.length++
} else {
node.next = this.head
this.head.prev = node
this.head = node
this.length++
}
}
deleteAtTail() {
if (!this.head) throw new Error("Empty Linked List!")
if (this.length === 1) {
this.head = null
this.tail = null
this.length--
} else {
this.tail = this.tail.prev
this.tail.next = null
this.length--
}
}
deleteAtHead() {
if (!this.head) throw new Error("Empty Linked List!")
if (this.length === 1) {
this.head = null
this.tail = null
this.length--
} else {
this.head = this.head.next
this.head.prev = null
this.length--
}
}
print() {
if (!this.head) console.log("Empty Linked List!")
else {
let curr = this.head
let str = "null <- "
while (curr) {
str += curr.val
if (curr.next) {
str += " <-> "
} else {
str += " -> null"
}
curr = curr.next
}
console.log(str)
}
}
getLength() {
return this.length
}
getAtIdx(idx) {
if (idx >= this.length || idx < 0) throw new Error("Index out of bounds!")
else {
let currIdx = 0
let curr = this.head
while (currIdx < idx) {
curr = curr.next
currIdx++
}
return curr.val
}
}
putAtIdx(val, idx) {
if (idx > this.length || idx < 0) throw new Error("Index out of bounds!")
const node = new Node(val)
if (idx === 0) {
this.head.prev = node
node.next = this.head
this.head = node
} else {
let currIdx = 0
let curr = this.head
while (currIdx < idx) {
curr = curr.next
currIdx++
}
node.next = curr
node.prev = curr.prev
curr.prev.next = node
}
this.length++
}
deleteAtIdx(idx) {
if (idx >= this.length || idx < 0) throw new Error("Index out of bounds!")
let currIdx = 0
let curr = this.head
while (currIdx < idx) {
curr = curr.next
currIdx++
}
curr.prev.next = curr.next
curr.next.prev = curr.prev
this.length--
}
search(val) {
let curr = this.head
let currIdx = 0
while (curr) {
if (curr.val === val) return currIdx
curr = curr.next
currIdx++
}
return "Value not found"
}
toArray() {
let arr = []
let curr = this.head
while (curr) {
arr.push(curr.val)
curr = curr.next
}
return arr
}
}
// SAMPLE USECASE
const list = new DoublyLinkedList ()
list.insertAtHead (10)
list.insertAtTail (20)
list.insertAtHead (30)
list.insertAtTail (40)
list.putAtIdx (100, 2)
list.print ()