-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdoubleLinkedList.js
More file actions
136 lines (117 loc) · 3.49 KB
/
doubleLinkedList.js
File metadata and controls
136 lines (117 loc) · 3.49 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
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
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoubleLinkedList {
constructor() {
this.head = null
this.tail = null;
this.length = 0;
this.current = null;
}
push(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
this.length = 1;
this.current = this.head;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
this.length++;
}
}
pop() {
if (!this.head) return null;
const previousNode = this.tail.prev;
const value = this.tail.value;
if (previousNode) {
if (this.current === this.tail) {
this.current = previousNode;
}
previousNode.next = null;
this.tail = previousNode;
this.length--;
} else {
this.head = null;
this.tail = null;
this.length = 0;
this.current = null;
}
return { value, start: this.current === this.head, end: this.current === this.tail} ;
}
insertAtBegining(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
this.length = 1;
} else {
this.head.prev = node;
node.next = this.head;
this.head = node;
this.length++;
}
}
removeFirst() {
if (!this.head) return null;
const node = this.head.next;
if (node) {
node.prev = null;
this.head = node;
this.current = this.head;
this.length--;
} else {
this.head = null;
this.tail = null;
this.length = 0;
this.current = null;
}
}
contains(value) {
if (!this.head) return false;
let position = this.head;
while (position) {
if (position.value === value) return true;
position = position.next;
}
return false;
}
next() {
if (this.length === 0) return null;
if (!this.current.next) return {value: null, end: true, start: this.current.prev === null};
const { value } = this.current;
this.current = this.current.next;
return {value, end: this.current.next === null, start: this.current.prev === null}
}
prev() {
if (this.length === 0) return null;
if (!this.current.prev) return {value: null, end:this.current.next === null, start: true };
const {value} = this.current;
this.current = this.current.prev;
return {value, end: this.current.next === null, start:this.current === null};
}
moveToStart() {
this.current = this.head;
return this.currentValue();
}
moveToEnd() {
this.current = this.tail;
return this.currentValue();
}
/**
* Return null if the current item is null - should only happen in empty list
* Otherwise returns an object with the value, and boolean for start and end of list
*/
currentValue() {
if (!this.current) return null;
let {value, next } = this.current;
return {value, end: this.current === this.tail, start: this.current === this.head}
}
}
module.exports = DoubleLinkedList;