-
Notifications
You must be signed in to change notification settings - Fork 109
/
DoubleLinkedList.kt
250 lines (210 loc) · 5.63 KB
/
DoubleLinkedList.kt
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package structures
/**
*
* LinkedList a data structure consisting of a collection of nodes that contain a link to the next/previous node.
*
* In the double LinkedList each node contains a link to the previous and next elements
*
*/
class DoubleLinkedList<T> {
private var head: Node<T>? = null
private var tail: Node<T>? = null
private var size: Int = 0
val isEmpty: Boolean
get() = head == null
// Complexity: O(n)
val list: List<T>
get() {
val nodes = mutableListOf<T>()
var node = head
while (node != null) {
val nodeValue = node.value()
if (nodeValue != null) {
nodes.add(nodeValue)
}
node = node.next()
}
return nodes
}
// Complexity: O(n)
val reversedList: List<T>
get() {
val nodes = mutableListOf<T>()
var node = tail
while (node != null) {
val nodeValue = node.value()
if (nodeValue != null) {
nodes.add(nodeValue)
}
node = node.previous()
}
return nodes
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun add(index: Int, value: T) : Boolean {
if (head == null) return false
var i = 0
var node = head
var prevNode = head
while (prevNode != null && node != null) {
if (i == index) {
val newNode = Node(value)
newNode.changeNext(node)
newNode.changePrevious(prevNode)
prevNode.changeNext(newNode)
size++
return true
}
i++
prevNode = node
node = node.next()
}
return false
}
fun add(value: T) = addLast(value)
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun addFirst(value: T) {
val headNode = head
head = if (headNode == null) {
Node(value)
} else {
val newNode = Node(value = value, next = headNode)
headNode.changePrevious(newNode)
newNode
}
if (tail == null) {
tail = head
}
size++
}
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun addLast(value: T) {
val tailNode = tail
tail = if (tailNode == null) {
Node(value)
} else {
val newNode = Node(value = value, previous = tailNode)
tailNode.changeNext(newNode)
newNode
}
if (head == null) {
head = tail
}
size++
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun contains(value: T) : Boolean {
if (head == null) return false
var node = head
while (node != null) {
if (node.value() == value) {
return true
}
node = node.next()
}
return false
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun remove(value: T) : Boolean {
if (head == null) return false
var previous: Node<T>? = null
var node = head
while (node != null) {
if (node.value() == value) {
val nextNode = node.next()
previous?.changeNext(nextNode)
nextNode?.changePrevious(previous)
if (node === head) {
head = nextNode
}
if (node === tail) {
tail = previous
}
node.changePrevious(null)
node.changeNext(null)
node.changeValue(null)
size--
return true
}
previous = node
node = node.next()
}
return false
}
// Complexity: O(n)
fun clear() {
var node = head
while (node != null) {
val currentNode = node
node = node.next()
currentNode.changeNext(null)
currentNode.changePrevious(null)
currentNode.changeValue(null)
}
head = null
tail = null
size = 0
}
override fun toString(): String {
val builder = StringBuilder()
builder.append("size: $size\n")
builder.append("elements: ")
var node = head
while (node != null) {
// it's necessary to see the correct node connections
if (node.previous() != null) {
builder.append("- ")
}
builder.append(node.value())
// it's necessary to see the correct node connections
if (node.next() != null) {
builder.append(" -")
}
node = node.next()
}
return builder.toString()
}
class Node<T>(
private var value: T? = null,
private var previous: Node<T>? = null,
private var next: Node<T>? = null
) {
fun next() = next
fun changeNext(node: Node<T>? = null) {
next = node
}
fun previous() = previous
fun changePrevious(node: Node<T>? = null) {
previous = node
}
fun value() = value
fun changeValue(newValue: T?) {
value = newValue
}
}
}