Skip to content

Commit

Permalink
chore(examples): add linked list example
Browse files Browse the repository at this point in the history
  • Loading branch information
saffage committed Jun 16, 2024
1 parent 20896d3 commit 5985f66
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/linked_list.jet
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Node :: struct {
data: int
next: pointer # because recursive definitions is not yet supported
}

print_tree :: (tree: *Node) {
if tree != { 0 as *Node } {
$print(tree.*.data)
tree = tree.*.next as *Node
}

while tree != { 0 as *Node } {
$print(" -> ")
$print(tree.*.data)
tree = tree.*.next as *Node
}
}

@[extern_c("malloc")]
alloc: (size: u64) -> pointer

main :: () {
tree := alloc($size_of(Node)) as *Node
two := alloc($size_of(Node)) as *Node
one := alloc($size_of(Node)) as *Node

tree.* = Node(data = 3, next = 0 as pointer)
two.* = Node(data = 2, next = tree as pointer)
one.* = Node(data = 1, next = two as pointer)

head := one
print_tree(head)
}

0 comments on commit 5985f66

Please sign in to comment.