-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedList.js
147 lines (115 loc) · 2.86 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
import Node from './node.js'
class LinkedList {
#head
#length
constructor (elements = []) {
if (!Array.isArray(elements)) throw Error('Invalid queue constructor')
this.#head = null
this.#length = elements.length
this.#buildList(elements)
}
push (element) {
const node = new Node(element)
if (this.#head === null) {
this.#head = node
} else {
let current = this.#head
while (current.next !== null) {
current = current.next
}
current.next = node
}
this.#length++
}
insert (element, position) {
const node = new Node(element)
if (position === 0) {
node.next = this.#head
this.#head = node
} else {
const previous = this.getElementAt(position - 1)
if (previous === undefined) throw Error('The position does not exist')
node.next = previous.next
previous.next = node
}
this.#length++
}
getElementAt (index) {
if (index > this.#length) return undefined
let currentNode = this.#head
for (let i = 0; i < index; i++) {
currentNode = currentNode.next
}
return currentNode
}
remove (element) {
if (this.isEmpty) return undefined
if (element === this.#head.value) {
this.#head = this.#head.next
} else {
let lastNode = this.#head
let currentNode = this.#head.next
while (currentNode?.value !== element) {
lastNode = currentNode
currentNode = currentNode.next
}
if (!currentNode) return undefined
lastNode.next = currentNode.next
currentNode = null
}
this.#length--
}
indexOf (element) {
let currentNode = this.#head
let counter = 0
while (currentNode && currentNode.value !== element) {
currentNode = currentNode.next
counter++
}
if (!currentNode) counter = -1
return counter
}
removeAt (position) {
if (position === 0) {
this.#head = this.#head.next
} else {
let lastNode = this.#head
let currentNode = this.#head
for (let i = 0; i < position; i++) {
lastNode = currentNode
currentNode = currentNode.next
}
lastNode.next = currentNode.next
currentNode = null
}
this.#length--
}
get isEmpty () {
return this.#head === null
}
get length () {
return this.#length
}
get head () {
return this.#head
}
#buildList (elements) {
if (elements.length === 0) return undefined
this.#head = new Node(elements[0])
let currentNode = this.#head
for (let i = 1; i < elements.length; i++) {
currentNode.next = new Node(elements[i])
currentNode = currentNode.next
}
}
toString () {
let string = ''
let current = this.#head
while (current) {
string += `${current.value},`
current = current.next
}
return string.slice(0, -1)
}
}
export default LinkedList