Jump to content

Singly-linked list/Element insertion: Difference between revisions

→‎{{header|Kotlin}}: Converted insertion function to 'stand alone' plus other minor changes
(Added Kotlin)
(→‎{{header|Kotlin}}: Converted insertion function to 'stand alone' plus other minor changes)
Line 854:
<lang scala>// version 1.1.1
 
class Node<T: Number>(var data: T, var next: Node<T>? = null) {
fun insertAfter(prev: Node<T>, new: Node<T>) {
new.next = prev.next
prev.next = new
}
override fun toString(): String {
val sb = StringBuilder(this.data.toString())
Line 869 ⟶ 864:
return sb.toString()
}
}
 
fun <T: funNumber> insertAfter(prev: Node<T>, new: Node<T>) {
new.next = prev.next
prev.next = new
}
 
fun main(args: Array<String>) {
val cb = Node(3)
val a = Node(1, cb)
println("Before insertion : $a")
val bc = Node(2)
a.insertAfter(a, bc)
println("After insertion : $a")
}</lang>
 
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.