diff --git a/kotlin/linked-list/src/main/kotlin/Deque.kt b/kotlin/linked-list/src/main/kotlin/Deque.kt index 1c9b366..9ca6533 100644 --- a/kotlin/linked-list/src/main/kotlin/Deque.kt +++ b/kotlin/linked-list/src/main/kotlin/Deque.kt @@ -1,61 +1,101 @@ class Deque { - var firstNode: Node? = null - var lastNode: Node? = null - // Remove value at back - fun pop(): T{ - val returnData = lastNode!!.data - lastNode = lastNode?.beforeNode - return returnData - } + private var head:Node? = null - // Add value at back - fun push(elem: T): Unit { - if (firstNode == null) { - firstNode = Node(data = elem, beforeNode = null, afterNode = null) + fun first():Node? = head + + fun last(): Node? { + var node = head + if (node != null){ + while (node?.next != null) { + node = node.next + } + return node } else { - if (lastNode == null){ - this.lastNode = Node(data = elem, beforeNode = firstNode, afterNode = null) - firstNode?.linkBefore(this.lastNode!!) - } - else { - this.lastNode?.linkBefore(Node(data = elem, beforeNode = this.lastNode, afterNode = null)) - } + return null } } - // Remove value at front - fun shift(): T{ - val returnData = firstNode!!.data - firstNode = firstNode?.afterNode - return returnData + fun nodeAtIndex(index: Int) : Node? { + if (index >= 0) { + var node = head + var i = index + while (node != null) { + if (i == 0) return node + i -= 1 + node = node.next + } + } + return null } - // Add value at front - fun unshift(elem: T): Unit { - if (firstNode == null) { - firstNode = Node(data = elem, beforeNode = null, afterNode = null) + fun push(value: T) { + val newNode = Node(value) + + val lastNode = this.last() + if (lastNode != null) { + newNode.previous = lastNode + lastNode.next = newNode } else { - if (lastNode == null){ - this.lastNode = this.firstNode - firstNode = Node(data = elem, beforeNode = null, afterNode = this.lastNode) - this.lastNode?.linkAfter(firstNode!!) - } - else { - this.firstNode?.linkAfter(Node(data = elem, beforeNode = null, afterNode = this.firstNode)) - } + head = newNode + } + } + + fun unshift(value: T) { + val newNode = Node(value) + + val firstNode = this.first() + if (firstNode != null){ + newNode.next = firstNode + firstNode.previous = newNode + head = newNode + } else { + head = newNode + } + } + + fun removeNode(node: Node):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(val data: R, var beforeNode: Node?, var afterNode: Node?) { - fun linkBefore(afterNode: Node): Unit { - this.afterNode = afterNode - afterNode.beforeNode = this - } - - fun linkAfter(beforeNode: Node): Unit { - this.beforeNode = beforeNode - beforeNode.afterNode = this - } +class Node(value: T){ + var value:T = value + var next: Node? = null + var previous:Node? = null } \ No newline at end of file