-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular-linked-list.js
195 lines (159 loc) · 3.57 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
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
191
192
193
194
195
'use strict';
class Node {
constructor (value, next = null, previous = null) {
this.value = value;
this.next = next;
this.previous = previous;
}
}
export default class {
constructor () {
this.last = null;
}
get values () {
let values = '';
if (!this.isEmpty()) {
let current = this.last.next;
do {
values += JSON.stringify(current.value) + ',';
current = current.next;
} while (current && current !== this.last.next);
values = values.slice(0, -1);
}
return values;
}
/**
* Adds a value to the end of the linked list
* @param {*}
*/
add (value) {
let n = new Node(value);
if (this.isEmpty()) {
this.last = n;
n.next = n;
n.previous = n;
} else {
let first = this.last.next;
this.last.next = n;
n.previous = this.last;
first.previous = n;
if (first === this.last) {
n.next = this.last;
} else {
n.next = first;
}
this.last = n;
}
}
/**
* Adds a value at first position of list
* @param {*} value
*/
addFirst (value) {
let n = new Node(value);
if (this.isEmpty()) {
this.add(value);
} else {
let first = this.last.next;
n.next = first;
n.previous = this.last;
first.previous = n;
this.last.next = n;
}
}
/**
* Adds a value into a specified position in the list
* @param {*} value
* @param {Number} position
*/
addAtPosition (value, position) {
let index = 1;
let current;
let n = new Node(value);
if (position === 0) {
this.addFirst(value);
return;
}
if (this.isEmpty() && position > 0) {
throw new Error('Unable to add at position ' + position + ', list is of length ' + (index === 1 ? 0 : index));
} else {
current = this.last.next;
}
while (index <= position) {
if (index < position) {
current = current.next;
index++;
} else if (index === position) {
let tmp = current.next;
current.next = n;
tmp.previous = n;
n.next = tmp;
n.previous = current;
break;
}
if (current === this.last.next) {
break;
}
}
if (n.previous === this.last) {
this.last = n;
}
if (index !== position && position !== 0) {
throw new Error('Unable to add at position ' + position + ', list is of length ' + (index === 1 ? 0 : index));
}
}
/**
* Search for a value in the list
* @param {*} value
* @return {Node}
*/
search (value) {
let current = this.last;
do {
if (current) {
if (current.value === value) {
return current;
}
current = current.next;
}
} while (current !== null && current !== this.last);
return false;
}
/**
* Delete a value from the list
* @param {*} value
* @return {Boolean}
*/
delete (value) {
let node = this.search(value);
if (node) {
node.previous.next = node.next;
node.next.previous = node.previous;
if (node === this.last) {
this.last = node.previous;
}
return true;
}
return false;
}
/**
* Reverse the linked list
*/
reverse () {
if (!this.isEmpty()) {
let first = this.last.next;
let current = first;
let tmp;
do {
tmp = current.next;
current.next = current.previous;
current.previous = tmp;
current = tmp;
} while (current !== first);
this.last = current;
}
}
isEmpty () {
return this.last === null;
}
}