-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_link_list.js
More file actions
76 lines (73 loc) · 2.16 KB
/
Copy pathdouble_link_list.js
File metadata and controls
76 lines (73 loc) · 2.16 KB
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
class Node{
constructor(value) {
this.prev = null;
this.value = value;
this.next = null;
}
}
//List holds a series of nodes
class List{
constructor() {
this.head = null;
this.tail = null;
}
//Create a node to be added
pushback(value) {
let newNode = new Node(value);
//If this is the first node
if (null == this.head ) {
this.head = newNode;
this.tail = newNode;
}
else {
newNode.prev = this.tail;
this.tail.next = newNode;//Add beyond last node
this.tail = newNode;//newNode becomes last node
}
}
printforward() {
for (let current = this.head; current; current=current.next){
console.log(current.value);
}
}
printBackward() {
for (let current = this.tail; current; current = current.prev) {
console.log(current.value);
}
}
removeNode(value) {
for (let current = this.head; current; current = current.next) {
if (value == current.value) {
//Got the value
//Value at start
if (current == this.head) {
this.head = this.head.next;
this.head.prev = null;
return true;
}
//Value at the end
if (current == this.tail) {
this.tail = this.tail.prev;
this.tail.next = null;
return true;
}
//Only one node
if (this.head == this.tail) {
this.head = null;
this.tail = null;
return true;
}
//Value somewhere in between
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
return false;//Could not find the value
}
}
let list = new List();
list.pushback(5);
list.pushback(1);
list.pushback(7);
list.removeNode(1);
list.printforward()