cleaned up the set of samples

This commit is contained in:
cozis
2022-03-08 22:20:04 +01:00
parent 542177e46f
commit 94dc01dcaa
12 changed files with 4 additions and 186 deletions
+37
View File
@@ -0,0 +1,37 @@
# Implementation of a stack using a linked list.
stack = {count: 0, head: none};
fun push(stack, value)
{
node = {prev: none, item: value};
node.prev = stack.head;
stack.head = node;
stack.count = stack.count + 1;
}
fun pop(stack)
{
if stack.head == none:
return none; # return it; ?
value = stack.head.item;
stack.head = stack.head.prev;
stack.count = stack.count - 1;
return value;
}
push(stack, 1);
push(stack, 2);
push(stack, 3);
x = pop(stack);
y = pop(stack);
z = pop(stack);
w = pop(stack);
assert(x == 3);
assert(y == 2);
assert(z == 1);
assert(w == none);