-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoDoublyLinkedList.js
172 lines (159 loc) · 4.12 KB
/
sudoDoublyLinkedList.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
// implementation of doubly linked list
class Node {
constructor(data){
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList{
constructor(){
this.head = null;
this.tail = null;
}
append(item){
let node = new Node(item);
if(!this.head){
this.head = node;
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
}
appendAt(pos, item){
let current = this.head;
let current = 1;
let node= new Node(items);
if(pos === 0){
this.head.prev = node;
node.next = this.head;
this.head = node;
} else {
while (current){
current = current.next;
if (counter = pos){
node.prev = current.prev;
current.prev.next = node;
node.next = current;
current.prev = node;
}
counter++;
}
}
}
// removing node
remove(item){
let current = this.head;
while(current){
if(current.data === item){
if(current == this.head && current == this.tail){
this.head = null;
this.tail = null;
} else if (current == this.head){
this.head = this.head.next;
this.head.prev = null;
} else if(current = this.tail){
this.tail = this.tail.prev;
this.tail.next = null;
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
current = current.next;
}
}
removeAt(pos){
let current= this.head;
let counter = 1;
if(pos == 0){
this.head = this.head.next;
this.head.prev = null;
} else {
while(current){
current = current.next;
if(current == this.tail){
this.tail = this.tail.prev;
this.tail.next = null;
} else if (counter == pos){
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
counter++;
}
}
}
reverse(){
let current = this.head;
let prev = null;
while(current){
let next = current.next;
current.next = prev;
current.prev = next;
prev = current;
current = next;
}
this.tail = this.head;
this.head = prev;
}
swap(nodeOne, nodeTwo){
let current = this.head;
let counter = 0;
let firstNode;
while (current !== null){
if(counter == nodeOne){
fistNode = current;
} else if(counter == nodeTwo){
let temp = current.data;
current.data = firstNode.data;
firstNode.data = temp;
}
current = current.next;
counter++;
}
return true;
}
length(){
let current = this.head;
let counter = 0;
while(current !== null){
counter++;
current = current.next;
}
return counter;
}
isEmpty(){
return this.length() < 1;
}
traverse(fn){
let current = this.head;
while(current !== null){
fn(current);
current = current.next;
}
return true;
}
traversereverse(fn){
let current = this.tail;
while(current !== null){
fn(current);
current = current.prev;
}
return true;
}
search(item){
let current = this.head;
let counter = 0;
while(current){
if(current.data == item){
return counter;
}
currnet = current.next;
counter++;
}
return false;
}
}