-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.kt
192 lines (155 loc) · 4.07 KB
/
LinkedList.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
package linkedlist.singlylinkedlist
class LinkedList<T> : Iterable<T>, Collection<T>,
MutableIterable<T>, MutableCollection<T> {
private var head: Node<T>? = null
private var tail: Node<T>? = null
override var size = 0
private set
override fun clear() {
head = null
tail = null
size = 0
}
override fun addAll(elements: Collection<T>): Boolean {
for (element in elements) {
append(element)
}
return true
}
override fun add(element: T): Boolean {
append(element)
return true
}
override fun containsAll(elements: Collection<T>): Boolean {
for (searched in elements) {
if (!contains(searched)) return false
}
return true
}
override fun contains(element: T): Boolean {
for (item in this) {
if (item == element) return true
}
return false
}
override fun iterator(): MutableIterator<T> {
return LinkedListIterator(this)
}
override fun retainAll(elements: Collection<T>): Boolean {
var result = false
val iterator = this.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (!elements.contains(item)) {
iterator.remove()
result = true
}
}
return result
}
override fun removeAll(elements: Collection<T>): Boolean {
var result = false
for (item in elements) {
result = remove(item) || result
}
return result
}
override fun remove(element: T): Boolean {
val iterator = iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (item == element) {
iterator.remove()
return true
}
}
return false
}
override fun isEmpty() : Boolean {
return (size == 0)
}
override fun toString(): String {
return if (isEmpty()) {
"Empty List"
} else {
head.toString()
}
}
fun push(value: T) : LinkedList<T> {
head = Node(value = value, next = head)
if (tail == null) {
tail = head
}
size++
return this
}
fun append(value: T) {
if (isEmpty()) {
push(value)
return
} else {
tail?.next = Node(value = value)
tail = tail?.next
size++
}
}
fun nodeAt(index: Int) : Node<T>? {
var current = head
var currentIndex = 0
while (current != null && currentIndex < index) {
current = current.next
currentIndex++
}
return current
}
fun insert(value: T, afterNode: Node<T>) : Node<T> {
if (tail == afterNode) {
append(value)
return tail!!
}
val newNode = Node(value = value, next = afterNode.next)
afterNode.next = newNode
size++
return newNode
}
fun insertWithIndex(value: T, index: Int) : Node<T> {
val node = nodeAt(index -1)
return insert(value, node!!)
}
fun pop(): T? {
if (!isEmpty()) size--
val result = head?.value
head = head?.next
if (isEmpty()) {
tail = null
}
return result
}
fun removeLast() : T? {
val head = head ?: return null
if (head.next == null) return pop()
size--
var prev = head
var current = head
var next = current.next
while (next != null) {
prev = current
current = next
next = current.next
}
prev.next = null
tail = prev
return current.value
}
fun removeAfter(node: Node<T>) : T? {
val result = node.next?.value
if (node.next == tail) {
tail = node
}
if (node.next != null) {
size--
}
node.next = node.next?.next
return result
}
}