Implement a Stack and a Queue in Larzscript
The two most basic linear data structures - last-in-first-out and first-in-first-out - named explicitly instead of just using a bare list.
Both are built directly on the language's own list push()/pop(); the packages exist so the intent ("this is a stack", "this is a queue") reads clearly in real code.
larzscript pkg install stack
larzscript pkg install queue
Code
import "stack" as stack
import "queue" as queue
let s = stack.new()
stack.push(s, 1); stack.push(s, 2); stack.push(s, 3)
print("stack pop:", stack.pop(s))
let q = queue.new()
queue.enqueue(q, 1); queue.enqueue(q, 2); queue.enqueue(q, 3)
print("queue dequeue:", queue.dequeue(q))Output
stack pop: 3 queue dequeue: 1
Related
larzstackA LIFO stack→larzqueueA FIFO queue→Implement a Binary Search Tree in LarzscriptInsert values into a binary search tree and read them back in sorted order.→Implement a Priority Queue (Binary Heap) in LarzscriptAlways get the smallest (or, with a key function, largest/custom-ordered) item next - the classic array-backed binary heap.→