Skip to content

Commit fbf6dad

Browse files
committed
Doubly linked list in js
1 parent 99b8d63 commit fbf6dad

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

Linked List/doubly_linked_list.js

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
class Node {
2+
constructor(val) {
3+
this.val = val
4+
this.prev = null
5+
this.next = null
6+
}
7+
}
8+
9+
class DoublyLinkedList {
10+
constructor() {
11+
this.head = null
12+
this.tail = null
13+
this.length = 0
14+
}
15+
16+
insertAtTail(val) {
17+
const node = new Node(val)
18+
19+
if (!this.head) {
20+
this.head = node
21+
this.tail = node
22+
this.length++
23+
} else {
24+
node.prev = this.tail
25+
this.tail.next = node
26+
this.tail = node
27+
this.length++
28+
}
29+
}
30+
31+
insertAtHead(val) {
32+
const node = new Node(val)
33+
34+
if (!this.head) {
35+
this.head = node
36+
this.tail = node
37+
this.length++
38+
} else {
39+
node.next = this.head
40+
this.head.prev = node
41+
this.head = node
42+
this.length++
43+
}
44+
}
45+
46+
deleteAtTail() {
47+
if (!this.head) throw new Error("Empty Linked List!")
48+
49+
if (this.length === 1) {
50+
this.head = null
51+
this.tail = null
52+
this.length--
53+
} else {
54+
this.tail = this.tail.prev
55+
this.tail.next = null
56+
this.length--
57+
}
58+
}
59+
60+
deleteAtHead() {
61+
if (!this.head) throw new Error("Empty Linked List!")
62+
63+
if (this.length === 1) {
64+
this.head = null
65+
this.tail = null
66+
this.length--
67+
} else {
68+
this.head = this.head.next
69+
this.head.prev = null
70+
this.length--
71+
}
72+
}
73+
74+
print() {
75+
if (!this.head) console.log("Empty Linked List!")
76+
77+
else {
78+
let curr = this.head
79+
let str = "null <- "
80+
while (curr) {
81+
str += curr.val
82+
if (curr.next) {
83+
str += " <-> "
84+
} else {
85+
str += " -> null"
86+
}
87+
curr = curr.next
88+
}
89+
console.log(str)
90+
}
91+
}
92+
93+
getLength() {
94+
return this.length
95+
}
96+
97+
getAtIdx(idx) {
98+
if (idx >= this.length || idx < 0) throw new Error("Index out of bounds!")
99+
100+
else {
101+
let currIdx = 0
102+
let curr = this.head
103+
104+
while (currIdx < idx) {
105+
curr = curr.next
106+
currIdx++
107+
}
108+
109+
return curr.val
110+
}
111+
}
112+
113+
putAtIdx(val, idx) {
114+
if (idx > this.length || idx < 0) throw new Error("Index out of bounds!")
115+
116+
const node = new Node(val)
117+
118+
if (idx === 0) {
119+
this.head.prev = node
120+
node.next = this.head
121+
this.head = node
122+
} else {
123+
let currIdx = 0
124+
let curr = this.head
125+
126+
while (currIdx < idx) {
127+
curr = curr.next
128+
currIdx++
129+
}
130+
131+
node.next = curr
132+
node.prev = curr.prev
133+
curr.prev.next = node
134+
}
135+
136+
this.length++
137+
}
138+
139+
deleteAtIdx(idx) {
140+
if (idx >= this.length || idx < 0) throw new Error("Index out of bounds!")
141+
142+
let currIdx = 0
143+
let curr = this.head
144+
145+
while (currIdx < idx) {
146+
curr = curr.next
147+
currIdx++
148+
}
149+
150+
curr.prev.next = curr.next
151+
curr.next.prev = curr.prev
152+
this.length--
153+
}
154+
155+
search(val) {
156+
let curr = this.head
157+
let currIdx = 0
158+
159+
while (curr) {
160+
if (curr.val === val) return currIdx
161+
162+
curr = curr.next
163+
currIdx++
164+
}
165+
166+
return "Value not found"
167+
}
168+
169+
toArray() {
170+
let arr = []
171+
172+
let curr = this.head
173+
174+
while (curr) {
175+
arr.push(curr.val)
176+
curr = curr.next
177+
}
178+
179+
return arr
180+
}
181+
}
182+
183+
// SAMPLE USECASE
184+
const list = new DoublyLinkedList ()
185+
list.insertAtHead (10)
186+
list.insertAtTail (20)
187+
list.insertAtHead (30)
188+
list.insertAtTail (40)
189+
list.putAtIdx (100, 2)
190+
list.print ()

0 commit comments

Comments
 (0)