Singly-linked list/Element definition: Difference between revisions
Singly-linked list/Element definition (view source)
Revision as of 11:39, 6 February 2024
, 3 months ago→{{header|Wren}}: Minor tidy
(Added Odin variant) |
m (→{{header|Wren}}: Minor tidy) |
||
(9 intermediate revisions by 7 users not shown) | |||
Line 398:
Return</syntaxhighlight>
=={{header|
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM node{pNext%, iData%}
Line 561 ⟶ 562:
=={{header|Elena}}==
ELENA 6.x
<syntaxhighlight lang="elena">class Link
{
constructor(int item, Link next)
Line 857 ⟶ 859:
else ($x.item) == ($y.item)
and equal_singly_linked_lists($x.next; $y.next)
end;
# insert $x into the front of the SLL
def insert($x):
if is_empty_singly_linked_list then {item: $x, next: null}
else .next |= new($x; .)
end;
</syntaxhighlight>
=={{header|Julia}}==
Line 946 ⟶ 955:
1 -> 2 -> 3
</pre>
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&Node = {
$next
$data
}
</syntaxhighlight>
=={{header|Logo}}==
Line 964 ⟶ 981:
<syntaxhighlight lang="mathematica">Append[{}, x]
-> {x}</syntaxhighlight>
=={{header|MiniScript}}==
Implementing linked lists in MiniScript is an academic exercise. For practical applications, use the built-in list type.
<syntaxhighlight lang="miniscript">
Node = {"item": null, "next": null}
Node.init = function(item)
node = new Node
node.item = item
return node
end function
LinkedList = {"head": null, "tail": null}
LinkedList.append = function(item)
newNode = Node.init(item)
if self.head == null then
self.head = newNode
self.tail = self.head
else
self.tail.next = newNode
self.tail = self.tail.next
end if
end function
LinkedList.insert = function(aftItem, item)
newNode = Node.init(item)
cursor = self.head
while cursor.item != aftItem
print cursor.item
cursor = cursor.next
end while
newNode.next = cursor.next
cursor.next = newNode
end function
LinkedList.traverse = function
cursor = self.head
while cursor != null
// do stuff
print cursor.item
cursor = cursor.next
end while
end function
test = new LinkedList
test.append("A")
test.append("B")
test.insert("A","C")
test.traverse
</syntaxhighlight>
=={{header|Modula-2}}==
Line 1,004 ⟶ 1,071:
=={{header|Nim}}==
<syntaxhighlight lang="nim">
import std/strutils # for join
type
Node[T] = ref object
next: Node[T]
Line 1,050 ⟶ 1,119:
for i in 1..5: list.append(i)
for i in 6..10: list.prepend(i)
echo "List: ", $list
</syntaxhighlight>
{{out}}
Line 1,906 ⟶ 1,976:
=={{header|Swift}}==
<syntaxhighlight lang="swift">class Node<T>{
var data: T
var next: Node? = nil
init(input: T){
data = input
next = nil
}
}
Line 1,948 ⟶ 2,018:
{{libheader|Wren-llist}}
The Node class in the above module is the element type for the LinkedList class which is a generic singly-linked list. The latter is implemented in such a way that the user does not need to deal directly with Node though for the purposes of the task we show below how instances of it can be created and manipulated.
<syntaxhighlight lang="
var n1 = Node.new(1)
|