-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
174 lines (141 loc) · 4.05 KB
/
linked-list.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
class Node {
constructor() {
this.value = undefined
this.next = undefined
this.prev = undefined
}
// isEqual(otherNode) {
// if(typeof(this.value) === 'object') {
// return this.value.value === otherNode.value.value
// }
// return this.value === otherNode.value
// }
}
class SingleLinkedList {
#length = 0
#head = undefined
#tail = undefined
all() {
if(this.#length === 0) {
console.log('there are no items in the linked list')
}
this.#iterate((node) => {
console.log('value ',node.value, ' prev: ', node.prev?.value, ' next: ', node.next?.value)
})
}
#iterate(job = (val) => true) {
let node = this.#head
for(let i = 0; i < this.#length; i++) {
let response = job(node)
if(response !== undefined || response === false) {
break
}
node = node.next
}
}
push(value) {
if(this.#length === 0) {
const node = new Node()
node.value = value
this.#head = node
this.#tail = node
} else {
const node = new Node()
node.value = value
node.prev = this.#tail
this.#tail.next = node
this.#tail = node
}
++this.#length
}
pop() {
if(this.#length === 0) {
return undefined
}
if(this.#length === 1) {
const item = this.#tail
this.#tail = undefined
this.#head = undefined
--this.#length
return item.value
}
let item = this.#tail
this.#tail = this.#tail.prev
this.#tail.next = undefined
--this.#length
return item.value
}
delete(value) {
if(this.#length === 0) {
console.log('currently there are no new elements')
return;
}
let prevLength = this.#length
this.#iterate((node) => {
if(node.value === value) {
debugger
--this.#length
// suppose we are deleting the first element of a linkedlist
if(node.prev === undefined) {
// check if there is a next node
if(this.#head.next === undefined) {
this.#head = undefined
this.#tail = undefined
} else {
this.#head = this.#head.next
this.#head.prev = null
}
return false
}
// lets suppose we are deleting the last element
if(node.next === undefined) {
this.#tail = node.prev
node.prev.next = undefined
return false
}
// deleting somewhere in the mid
node.prev.next = node.next
return false
}
})
if (this.#length === prevLength) {
return false
} else {
return true
}
}
insertAfter(search, value) {
if(this.#length === 0) {
console.log('currently there are no new elements')
return;
}
this.#iterate((node) => {
if(node.value === search) {
const newNode = new Node()
newNode.value = value
newNode.next = node.next
newNode.prev = node
node.next = newNode
++this.#length
return false
}
})
}
getlength() {
return this.#length
}
shift() {
if(this.#length <= 0) {
return
}
const prevHead = this.#head
this.#head = this.#head.next
this.#head.prev = null
--this.#length
return prevHead.value
}
getHead() {
return this.#head.value
}
}
module.exports = SingleLinkedList