38 lines
571 B
Plaintext
38 lines
571 B
Plaintext
# 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;
|
|
|
|
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);
|