Kotlin - LinkedList complete
This commit is contained in:
parent
4081083f82
commit
f6885e29f4
1 changed files with 86 additions and 46 deletions
|
@ -1,61 +1,101 @@
|
|||
class Deque<T> {
|
||||
var firstNode: Node<T>? = null
|
||||
var lastNode: Node<T>? = null
|
||||
|
||||
// Remove value at back
|
||||
fun pop(): T{
|
||||
val returnData = lastNode!!.data
|
||||
lastNode = lastNode?.beforeNode
|
||||
return returnData
|
||||
}
|
||||
private var head:Node<T>? = null
|
||||
|
||||
// Add value at back
|
||||
fun push(elem: T): Unit {
|
||||
if (firstNode == null) {
|
||||
firstNode = Node<T>(data = elem, beforeNode = null, afterNode = null)
|
||||
fun first():Node<T>? = head
|
||||
|
||||
fun last(): Node<T>? {
|
||||
var node = head
|
||||
if (node != null){
|
||||
while (node?.next != null) {
|
||||
node = node.next
|
||||
}
|
||||
return node
|
||||
} else {
|
||||
if (lastNode == 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))
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Remove value at front
|
||||
fun shift(): T{
|
||||
val returnData = firstNode!!.data
|
||||
firstNode = firstNode?.afterNode
|
||||
return returnData
|
||||
fun nodeAtIndex(index: Int) : Node<T>? {
|
||||
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<T>(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<T>(data = elem, beforeNode = null, afterNode = this.lastNode)
|
||||
this.lastNode?.linkAfter(firstNode!!)
|
||||
}
|
||||
else {
|
||||
this.firstNode?.linkAfter(Node<T>(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>):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>?) {
|
||||
fun linkBefore(afterNode: Node<R>): Unit {
|
||||
this.afterNode = afterNode
|
||||
afterNode.beforeNode = this
|
||||
}
|
||||
|
||||
fun linkAfter(beforeNode: Node<R>): Unit {
|
||||
this.beforeNode = beforeNode
|
||||
beforeNode.afterNode = this
|
||||
}
|
||||
class Node<T>(value: T){
|
||||
var value:T = value
|
||||
var next: Node<T>? = null
|
||||
var previous:Node<T>? = null
|
||||
}
|
Loading…
Add table
Reference in a new issue