Kotlin - LinkedList complete

This commit is contained in:
Anthony C 2017-08-02 11:52:38 -04:00
parent 4081083f82
commit f6885e29f4

View file

@ -1,61 +1,101 @@
class Deque<T> { class Deque<T> {
var firstNode: Node<T>? = null
var lastNode: Node<T>? = null
// Remove value at back private var head:Node<T>? = null
fun pop(): T{
val returnData = lastNode!!.data
lastNode = lastNode?.beforeNode
return returnData
}
// Add value at back fun first():Node<T>? = head
fun push(elem: T): Unit {
if (firstNode == null) { fun last(): Node<T>? {
firstNode = Node<T>(data = elem, beforeNode = null, afterNode = null) var node = head
if (node != null){
while (node?.next != null) {
node = node.next
}
return node
} else { } else {
if (lastNode == null){ return null
this.lastNode = Node<T>(data = elem, beforeNode = firstNode, afterNode = null)
firstNode?.linkBefore(this.lastNode!!)
}
else {
this.lastNode?.linkBefore(Node<T>(data = elem, beforeNode = this.lastNode, afterNode = null))
}
} }
} }
// Remove value at front fun nodeAtIndex(index: Int) : Node<T>? {
fun shift(): T{ if (index >= 0) {
val returnData = firstNode!!.data var node = head
firstNode = firstNode?.afterNode var i = index
return returnData while (node != null) {
if (i == 0) return node
i -= 1
node = node.next
}
}
return null
} }
// Add value at front fun push(value: T) {
fun unshift(elem: T): Unit { val newNode = Node(value)
if (firstNode == null) {
firstNode = Node<T>(data = elem, beforeNode = null, afterNode = null) val lastNode = this.last()
if (lastNode != null) {
newNode.previous = lastNode
lastNode.next = newNode
} else { } else {
if (lastNode == null){ head = newNode
this.lastNode = this.firstNode }
firstNode = Node<T>(data = elem, beforeNode = null, afterNode = this.lastNode) }
this.lastNode?.linkAfter(firstNode!!)
} fun unshift(value: T) {
else { val newNode = Node(value)
this.firstNode?.linkAfter(Node<T>(data = elem, beforeNode = null, afterNode = this.firstNode))
} val firstNode = this.first()
if (firstNode != null){
newNode.next = firstNode
firstNode.previous = newNode
head = newNode
} else {
head = newNode
}
}
fun removeNode(node: Node<T>):T {
val prev = node.previous
val next = node.next
if (prev != null) {
prev.next = next
} else {
head = next
}
next?.previous = prev
node.previous = null
node.next = null
return node.value
}
fun pop() : T? {
val last = this.last()
if (last != null) {
return removeNode(last)
} else {
return null
}
}
fun shift(): T? {
return removeAtIndex(0)
}
fun removeAtIndex(index: Int):T? {
val node = nodeAtIndex(index)
if (node != null) {
return removeNode(node)
} else {
return null
} }
} }
} }
data class Node<R>(val data: R, var beforeNode: Node<R>?, var afterNode: Node<R>?) { class Node<T>(value: T){
fun linkBefore(afterNode: Node<R>): Unit { var value:T = value
this.afterNode = afterNode var next: Node<T>? = null
afterNode.beforeNode = this var previous:Node<T>? = null
}
fun linkAfter(beforeNode: Node<R>): Unit {
this.beforeNode = beforeNode
beforeNode.afterNode = this
}
} }