Skip to content

Commit acbd43c

Browse files
Merge pull request akshitagit#67 from XenomShox/xenomshox_branch
Added Singly and Doubly Linked List Classes, with a bunch of methods(…
2 parents 72af288 + 270b364 commit acbd43c

File tree

3 files changed

+254
-1
lines changed

3 files changed

+254
-1
lines changed

DataStructures/DoublyLinkedList.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
class Node {
2+
constructor(val) {
3+
this.val = val;
4+
this.next = null;
5+
this.prev = null;
6+
}
7+
}
8+
9+
class DoublyLinkedList {
10+
constructor() {
11+
this.head = null;
12+
this.tail = null;
13+
this.length = 0;
14+
}
15+
push(val) {
16+
var newNode = new Node(val);
17+
if (this.length === 0) {
18+
this.head = newNode;
19+
this.tail = newNode;
20+
} else {
21+
this.tail.next = newNode;
22+
newNode.prev = this.tail;
23+
this.tail = newNode;
24+
}
25+
this.length++;
26+
return this;
27+
}
28+
pop() {
29+
if (!this.head) return undefined;
30+
var poppedNode = this.tail;
31+
if (this.length === 1) {
32+
this.head = null;
33+
this.tail = null;
34+
} else {
35+
this.tail = poppedNode.prev;
36+
this.tail.next = null;
37+
poppedNode.prev = null;
38+
}
39+
this.length--;
40+
return poppedNode;
41+
}
42+
shift() {
43+
if (this.length === 0) return undefined;
44+
var oldHead = this.head;
45+
if (this.length === 1) {
46+
this.head = null;
47+
this.tail = null;
48+
} else {
49+
this.head = oldHead.next;
50+
this.head.prev = null;
51+
oldHead.next = null;
52+
}
53+
this.length--;
54+
return oldHead;
55+
}
56+
unshift(val) {
57+
var newNode = new Node(val);
58+
if (this.length === 0) {
59+
this.head = newNode;
60+
this.tail = newNode;
61+
} else {
62+
this.head.prev = newNode;
63+
newNode.next = this.head;
64+
this.head = newNode;
65+
}
66+
this.length++;
67+
return this;
68+
}
69+
get(index) {
70+
if (index < 0 || index >= this.length) return null;
71+
var count, current;
72+
if (index <= this.length / 2) {
73+
count = 0;
74+
current = this.head;
75+
while (count !== index) {
76+
current = current.next;
77+
count++;
78+
}
79+
} else {
80+
count = this.length - 1;
81+
current = this.tail;
82+
while (count !== index) {
83+
current = current.prev;
84+
count--;
85+
}
86+
}
87+
return current;
88+
}
89+
set(index, val) {
90+
var foundNode = this.get(index);
91+
if (foundNode != null) {
92+
foundNode.val = val;
93+
return true;
94+
}
95+
return false;
96+
}
97+
insert(index, val) {
98+
if (index < 0 || index > this.length) return false;
99+
if (index === 0) return !!this.unshift(val);
100+
if (index === this.length) return !!this.push(val);
101+
102+
var newNode = new Node(val);
103+
var beforeNode = this.get(index - 1);
104+
var afterNode = beforeNode.next;
105+
106+
(beforeNode.next = newNode), (newNode.prev = beforeNode);
107+
(newNode.next = afterNode), (afterNode.prev = newNode);
108+
this.length++;
109+
return true;
110+
}
111+
}
112+
113+
var list = new DoublyLinkedList();
114+
list.push("Harry");
115+
list.push("Ron");
116+
list.push("Hermione");

DataStructures/SinglyLinkedList.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
class Node {
2+
constructor(val) {
3+
this.val = val;
4+
this.next = null;
5+
}
6+
}
7+
8+
class SinglyLinkedList {
9+
constructor() {
10+
this.head = null;
11+
this.tail = null;
12+
this.length = 0;
13+
}
14+
push(val) {
15+
var newNode = new Node(val);
16+
if (!this.head) {
17+
this.head = newNode;
18+
this.tail = this.head;
19+
} else {
20+
this.tail.next = newNode;
21+
this.tail = newNode;
22+
}
23+
this.length++;
24+
return this;
25+
}
26+
pop() {
27+
if (!this.head) return undefined;
28+
var current = this.head;
29+
var newTail = current;
30+
while (current.next) {
31+
newTail = current;
32+
current = current.next;
33+
}
34+
this.tail = newTail;
35+
this.tail.next = null;
36+
this.length--;
37+
if (this.length === 0) {
38+
this.head = null;
39+
this.tail = null;
40+
}
41+
return current;
42+
}
43+
shift() {
44+
if (!this.head) return undefined;
45+
var currentHead = this.head;
46+
this.head = currentHead.next;
47+
this.length--;
48+
if (this.length === 0) {
49+
this.tail = null;
50+
}
51+
return currentHead;
52+
}
53+
unshift(val) {
54+
var newNode = new Node(val);
55+
if (!this.head) {
56+
this.head = newNode;
57+
this.tail = this.head;
58+
}
59+
newNode.next = this.head;
60+
this.head = newNode;
61+
this.length++;
62+
return this;
63+
}
64+
get(index) {
65+
if (index < 0 || index >= this.length) return null;
66+
var counter = 0;
67+
var current = this.head;
68+
while (counter !== index) {
69+
current = current.next;
70+
counter++;
71+
}
72+
return current;
73+
}
74+
set(index, val) {
75+
var foundNode = this.get(index);
76+
if (foundNode) {
77+
foundNode.val = val;
78+
return true;
79+
}
80+
return false;
81+
}
82+
insert(index, val) {
83+
if (index < 0 || index > this.length) return false;
84+
if (index === this.length) return !!this.push(val);
85+
if (index === 0) return !!this.unshift(val);
86+
87+
var newNode = new Node(val);
88+
var prev = this.get(index - 1);
89+
var temp = prev.next;
90+
prev.next = newNode;
91+
newNode.next = temp;
92+
this.length++;
93+
return true;
94+
}
95+
remove(index) {
96+
if (index < 0 || index >= this.length) return undefined;
97+
if (index === 0) return this.shift();
98+
if (index === this.length - 1) return this.pop();
99+
var previousNode = this.get(index - 1);
100+
var removed = previousNode.next;
101+
previousNode.next = removed.next;
102+
this.length--;
103+
return removed;
104+
}
105+
reverse() {
106+
var node = this.head;
107+
this.head = this.tail;
108+
this.tail = node;
109+
var next;
110+
var prev = null;
111+
for (var i = 0; i < this.length; i++) {
112+
next = node.next;
113+
node.next = prev;
114+
prev = node;
115+
node = next;
116+
}
117+
return this;
118+
}
119+
print() {
120+
var arr = [];
121+
var current = this.head;
122+
while (current) {
123+
arr.push(current.val);
124+
current = current.next;
125+
}
126+
console.log(arr);
127+
}
128+
}
129+
130+
var list = new SinglyLinkedList();
131+
132+
list.push(100);
133+
list.push(201);
134+
list.push(250);
135+
list.push(350);
136+
list.push(999);
137+
138+
list.print();

DataStructures/add

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)