-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
circular-linked-list.js
150 lines (122 loc) · 3.15 KB
/
circular-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
class CircularLinkedList extends LinkedList {
push(element) {
const node = this.createNode(element);
if (!this.head) {
this.head = node;
} else {
const current = this.getNodeAt(this.size - 1);
current.next = node;
}
node.next = this.head;
this.size += 1;
return this.size;
}
insert(element, index = 0) {
if (index > this.size) return false;
const node = this.createNode(element);
if (index === 0) {
node.next = this.head;
if(this.isEmpty) {
node.next = node;
} else {
const last = this.getNodeAt(this.size - 1);
last.next = node;
}
this.head = node;
} else {
const previous = this.getNodeAt(index - 1);
node.next = previous.next;
previous.next = node;
}
this.size += 1;
return true;
}
remove(index = 0) {
if (index < 0 || index > this.size) return null;
let removedNode = this.head;
if (index === 0) {
if (this.size === 1) {
this.head = null;
} else {
const last = this.getNodeAt(this.size - 1);
this.head = this.head.next;
last.next = this.head;
}
} else {
let previous = this.getNodeAt(index - 1);
removedNode = previous.next;
previous.next = removedNode.next;
}
this.size -= 1;
return removedNode.element;
}
}
class CircularDoubleLinkedList extends DoubleLinkedList {
push(element) {
const node = this.createNode(element);
if (!this.head) {
this.head = node;
} else {
const current = this.getNodeAt(this.size - 1);
current.next = node;
node.prev = current;
}
this.tail = node;
this.tail.next = this.head;
this.size += 1;
return this.size;
}
insert(element, index = 0) {
if (index > this.size) return false;
const node = this.createNode(element);
if (index === 0) {
if (this.head) {
node.next = this.head;
this.head.prev = node;
} else {
this.tail = node;
}
this.head = node;
this.tail.next = this.head;
} else if (index === this.size) {
this.tail.next = node;
node.prev = this.tail;
node.next = this.head;
this.tail = node;
} else {
const current = this.getNodeAt(index);
const prev = current.prev;
prev.next = node;
current.prev = node;
node.prev = prev;
node.next = current;
}
this.size = this.size + 1;
return true;
}
remove(index = 0) {
if (index > this.size) return null;
let removedNode = this.head;
if (index === 0) {
this.head = removedNode.next;
if (this.size === 1) {
this.tail = null;
} else {
this.head.prev = null;
this.tail.next = this.head;
}
} else if (index === this.size - 1) {
removedNode = this.tail;
this.tail = removedNode.prev;
this.tail.next = this.head;
} else {
removedNode = this.getNodeAt(index);
const previous = removedNode.prev;
const next = removedNode.next;
previous.next = next;
next.prev = previous;
}
this.size = this.size - 1;
return removedNode;
}
}