-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublylinkedlist.js
More file actions
154 lines (153 loc) · 4.15 KB
/
doublylinkedlist.js
File metadata and controls
154 lines (153 loc) · 4.15 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Node{
constructor(val){
this.val = val;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val){
let newNode = new Node(val);
if (!this.head){
this.head = newNode;
this.tail = newNode;
}else{
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length++;
return this;
}
pop(){
let poppedNode = this.tail;
if (!this.head){
return false;
}else if (this.length === 1){
this.head = null;
this.tail = null;
}else {
let oldTail = this.tail;
this.tail = oldTail.prev;
this.tail.next = null;
oldTail.prev = null;
}
this.length--;
return poppedNode;
}
shift(){
let oldHead = this.head;
if (this.length === 0){
return false;
}else if (this.length === 1){
this.head = null;
this.tail = null;
}else{
this.head = oldHead.next;
this.head.prev = null;
oldHead.next = null;
}
this.length--;
return oldHead;
}
unshift(val){
let newNode = new Node(val);
if (!this.head){
this.head = newNode;
this.tail = newNode;
}else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
this.length++;
return newNode;
}
get(index){
if (index < 0 || index >= this.length){
return false;
}
let compare = Math.floor(index/this.length);
if (index <= compare){
let counter = 0;
let current = this.head;
while (counter !== index){
current =current.next;
counter++;
}
return current;
}else{
let counter = this.length - 1;
let current = this.tail;
while(counter !== index){
current = current.prev;
counter--;
}
return current;
}
}
set(index, val){
if (index < 0 || index >= this.length){
return false;
}else{
let current = this.get(index);
current.val = val;
return current;
}
}
insert(index, val){
if (index < 0 || index > this.length){
return false;
}else if (index === 0){
let current = this.unshift(val);
console.log("Added at the beginning");
return current;
}else if (index === this.length){
let current = this.push(val);
console.log("Added at the end");
return current;
}else {
let newNode = new Node(val);
let currentNode = this.get(index);
let prevNode = currentNode.prev;
prevNode.next = newNode;
newNode.prev = prevNode;
newNode.next = currentNode;
currentNode.prev = newNode;
this.length++;
return newNode;
}
}
remove(index){
if (index < 0 || index >= this.length){
return false;
}else if (index === 0){
let current = this.shift();
console.log("Removed from the beginning");
return current;
}else if (index === this.length -1){
let current = this.pop();
console.log("Removed from the end");
return current;
}else {
let currentNode = this.get(index);
let prevNode = currentNode.prev;
let nextNode = currentNode.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
currentNode.prev = null;
currentNode.next = null;
this.length--;
return this;
}
}
}
let double = new DoublyLinkedList();
double.push(5);
double.push(6);
double.push(7);