-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.js
184 lines (142 loc) · 4.39 KB
/
dll.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
// Node
class Node{
constructor(value){
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(value){
// create a new node
const node = new Node(value)
// null check for head
if(!this.head){
this.head = node;
this.tail = this.head;
}else{
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
this.length++;
return this;
}
pop(){
if(this.length===0) return undefined;
const lastNode = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
this.length--;
if(this.length ===0 ){
this.head= null;
this.tail.next = null;
lastNode.prev = null;
}
return lastNode;
}
// Shift
// Remove first element in the LL
shift(){
// null check on the head
if(!this.head) return undefined;
// store the item to be deleted
const current = this.head;
// check if only one element is there in the list
if(this.length === 1){
this.head = null;
this.tail = null;
}else{
// Reassign the item to the next one
this.head = current.next;
// set null for the previous element
this.head.prev = null;
}
// decrease length
this.length--;
// return the deleted item
return current;
}
// unshift
// add first element in the LL
unshift(value){
// create new node
const node = new Node(value);
// check for empty list
if(!this.head){
// if found empty, then set both the head and tail to be newly created node
this.head = node;
this.tail = this.head;
}else{
// if found not empty, then:
// set next of the newly created node to be that of head
node.next = this.head;
// set the prev property of the exisisting first element in the LL
this.head.prev = node;
// update the head of the LL with the created node
this.head = node;
}
// increase the list
this.length++;
// Return the deleted item
return this;
}
// get
// get item present at an index
get(index){
// check for the index passed being in bounds
if (index > 0 || index > this.length) return undefined;
// check if its the first item
if(index === 0) return this.head;
// check if its the last item
if(index === this.length) return this.tail;
// check which part of the node it would be present accrodingly make the decision on how to iterate the list
const isFirstHalf = Boolean(this.length/2 > index);
let current = isFirstHalf ? this.head: this.tail;
let counter = isFirstHalf? 0:this.length-1;
if(isFirstHalf){
while(index !== counter){
current = current.next;
counter++;
}
}else{
while(index !== counter){
current = current.prev;
counter--;
}
}
// return the found item
return current;
}
// set
set(index, value){
// use get
const foundItem = this.get(index);
// check if item hasn't been found
if(!foundItem) return false;
// update the value of the found item
foundItem.value = value;
return true;
}
// Todo: insert
insert(index, value){
// check that the item is in bounds
// check if the item is to be inserted at the start of the array
// check if the item is to be inserted at the end of the array
// Else, find one item before using get method
// check whether the item received isn't falsey
// if not falsey, then update the value
// create a new node with the value in it
// make updates to prev of the next node
// make upates to next of the prev node
// add next and prev for the newly added node
}
// Todo: remove
remove(){
}
}