-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.js
172 lines (147 loc) · 3.97 KB
/
LinkedList.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
"use strict";
// This iterator returns LinkedListNodes
// Usage: for (node of myLinkedList.getNodes()) { // do something }
function nodeIterator(self) {
return function* () {
let next = linkedLists.get(self).first
while (next) {
yield next
next = next.getNext()
}
}
}
// Using WeakMap to implement private variables
// See the excellent blog post about this technique here: http://fitzgeraldnick.com/weblog/53/
let linkedLists = new WeakMap()
class LinkedList {
constructor() {
// first and last will be private variables
// if you want to add your own private methods, instead of setting this to an empty object,
// you can instatiate from a class only visible in this module
linkedLists.set(this, {
first: null,
last: null
})
}
// This iterator returns the value in each node
// Usage: for (node of myLinkedList) { // do something }
*[Symbol.iterator]() {
let next = linkedLists.get(this).first
while (next) {
yield next.value
next = next.getNext()
}
}
getNodes() {
return {
[Symbol.iterator]: nodeIterator(this)
}
}
getFirst() {
let first = linkedLists.get(this).first
return first ? first.value : null
}
getLast() {
let last = linkedLists.get(this).last
return last ? last.value : null
}
push(value) {
let pv = linkedLists.get(this)
if (pv.last) {
let n = new LinkedListNode(value)
n.setPrevious(pv.last)
pv.last.setNext(n)
pv.last = n
} else {
pv.first = pv.last = new LinkedListNode(value)
}
return pv.last
}
pop() {
let ret,
prev,
pv = linkedLists.get(this)
if (pv.last) {
ret = pv.last.getValue()
if (pv.first === pv.last) {
pv.first = null
pv.last = null
} else {
prev = pv.last.getPrevious()
prev.setNext(null)
pv.last = prev
}
} else {
ret = null
}
return ret
}
unshift(value) {
let lln = new LinkedListNode(value),
pv = linkedLists.get(this)
if (pv.first) {
lln.setNext(pv.first)
pv.first = pv.first.setPrevious(lln)
} else {
pv.first = lln
pv.last = lln
}
}
shift() {
let ret,
pv = linkedLists.get(this)
if (pv.first) {
ret = pv.first.getValue()
pv.first = pv.first.getNext()
if (pv.first) {
pv.first.setPrevious(null)
}
} else {
ret = null
}
return ret
}
render() {
// Iterate over the nodes of the LinkedList and print the prev / value / next
// Custom iteration of the object is accomplished by defining the Symbol.iterator method above
for (let n of this.getNodes()) {
console.log(n.toString())
}
}
}
class LinkedListNode {
constructor(value) {
this.value = value
this.next = null
this.previous = null
}
getValue() {
return this.value
}
getPrevious() {
return this.previous
}
getNext() {
return this.next
}
setPrevious(previous) {
this.previous = previous
return previous
}
setNext(next) {
this.next = next
return next
}
toString() {
let prevStr = this.previous ? "<LinkedListNode: " + this.previous.value + ">" : "null"
let nextStr = this.next ? "<LinkedListNode: " + this.next.value + ">" : "null"
return `<LinkedListNode>
| Previous: ${prevStr}
| Value: ${this.value}
| Next: ${nextStr}`
}
}
module.exports = {
LinkedList: LinkedList,
LinkedListNode: LinkedListNode
}