Singly-linked list/Element insertion: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Added Odin variant) |
||
Line 1,591: | Line 1,591: | ||
| c :: cs -> c :: insert_after a b cs |
| c :: cs -> c :: insert_after a b cs |
||
| [] -> raise Not_found</syntaxhighlight> |
| [] -> raise Not_found</syntaxhighlight> |
||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="odin">package main |
|||
Node :: struct { |
|||
data: rune, |
|||
next: ^Node, |
|||
} |
|||
insert_after :: proc(node, new_node: ^Node) { |
|||
new_node.next = node.next |
|||
node.next = new_node |
|||
} |
|||
main :: proc() { |
|||
a := new(Node) |
|||
a.data = 'A' |
|||
b := new(Node) |
|||
b.data = 'B' |
|||
c := new(Node) |
|||
c.data = 'C' |
|||
insert_after(a, b) // A -> B |
|||
insert_after(a, c) // A -> C -> B |
|||
assert(a.data == 'A') |
|||
assert(a.next.data == 'C') |
|||
assert(a.next.next.data == 'B') |
|||
}</syntaxhighlight> |
|||
=={{header|Oforth}}== |
=={{header|Oforth}}== |