Doubly-linked list/Definition: Difference between revisions
Content added Content deleted
No edit summary |
(Added Kotlin) |
||
Line 1,421: | Line 1,421: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
See [[Doubly-Linked List (element)#JavaScript]], [[Doubly-Linked List (element insertion)#JavaScript]] and [[Doubly-Linked List (traversal)#JavaScript]] |
See [[Doubly-Linked List (element)#JavaScript]], [[Doubly-Linked List (element insertion)#JavaScript]] and [[Doubly-Linked List (traversal)#JavaScript]] |
||
=={{header|Kotlin}}== |
|||
Rather than use the java.util.LinkedList<E> class, we will write our own simple LinkedList<E> class for this task: |
|||
<lang scala>// version 1.1.1 |
|||
class LinkedList<E> { |
|||
class Node<E>(var data: E, var prev: Node<E>? = null, var next: Node<E>? = null) { |
|||
override fun toString(): String { |
|||
val sb = StringBuilder(this.data.toString()) |
|||
var node = this.next |
|||
while (node != null) { |
|||
sb.append(" -> ", node.data.toString()) |
|||
node = node.next |
|||
} |
|||
return sb.toString() |
|||
} |
|||
} |
|||
var first: Node<E>? = null |
|||
var last: Node<E>? = null |
|||
fun addFirst(value: E) { |
|||
if (first == null) { |
|||
first = Node(value) |
|||
last = first |
|||
} |
|||
else { |
|||
val node = first!! |
|||
first = Node(value, null, node) |
|||
node.prev = first |
|||
} |
|||
} |
|||
fun addLast(value: E) { |
|||
if (last == null) { |
|||
last = Node(value) |
|||
first = last |
|||
} |
|||
else { |
|||
val node = last!! |
|||
last = Node(value, node, null) |
|||
node.next = last |
|||
} |
|||
} |
|||
fun insert(after: Node<E>?, value: E) { |
|||
if (after == null) |
|||
addFirst(value) |
|||
else if (after == last) |
|||
addLast(value) |
|||
else { |
|||
val next = after.next |
|||
val new = Node(value, after, next) |
|||
after.next = new |
|||
if (next != null) next.prev = new |
|||
} |
|||
} |
|||
override fun toString() = first.toString() |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val ll = LinkedList<Int>() |
|||
ll.addFirst(1) |
|||
ll.addLast(4) |
|||
ll.insert(ll.first, 2) |
|||
ll.insert(ll.last!!.prev, 3) |
|||
println(ll) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 -> 2 -> 3 -> 4 |
|||
</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |