Linked lists, heaps, stacks, queues, deques, priority queues, and trees
SPL provides built-in mutable containers under the namespaces
stack,
queue,
deque,
priorityQueue,
linkedList,
heap, and
tree.
Unlike list.createList([...], name), each
create* call takes only a
variable name (identifier): the structure
starts empty and you add elements with the operations below.
Use type.isStack,
type.isQueue,
type.isDeque,
type.isPriorityQueue,
type.isLinkedList,
type.isHeap,
type.isTree, and
type.isTreeNode to branch at runtime.
Typed printing and returns use
print.stack …
print.treeNode and
return.stack …
return.treeNode.
LIFO: stack.createStack(Name); then
stack.push,
stack.pop,
stack.peek,
stack.size,
stack.isEmpty.
FIFO: queue.createQueue(Name); then
queue.enqueue,
queue.dequeue,
queue.peekFront,
queue.size,
queue.isEmpty.
Insert and remove at both ends:
deque.createDeque(Name); then
deque.pushFront /
deque.pushBack,
deque.popFront /
deque.popBack,
deque.peekFront /
deque.peekBack,
deque.size,
deque.isEmpty.
Useful for sliding windows, BFS frontiers, or any pattern that needs O(1) access at both ends.
Min-priority queue (lower numeric priority = served first):
priorityQueue.createPriorityQueue(Name); then
priorityQueue.push(pq, priority, value),
priorityQueue.pop,
priorityQueue.peek,
priorityQueue.size,
priorityQueue.isEmpty.
Ties on priority are broken FIFO (first pushed wins).
Ideal for Dijkstra-style search, task schedulers, and “always take the best pending item” loops.
Doubly linked: linkedList.createLinkedList(Name);
then appendFront /
appendBack,
popFront /
popBack,
peekFront /
peekBack,
size,
isEmpty.
Circular lists:
linkedList.makeCircular(list) connects tail to head
(and head.prev to tail). Use linkedList.isCircular
to test. Pops update links correctly when the list is circular.
Binary heap: heap.createHeap(Name); or
heap.createMinHeap(Name); for a min-heap;
heap.createMaxHeap(Name); for a max-heap.
Only numeric values (int/float, not bool) may be pushed.
Operations: heap.push,
heap.popTop,
heap.peekTop,
heap.size,
heap.isEmpty.
tree.createTree(Name); creates an empty tree
with a root you can set. Build manually with
tree.newNode(value),
tree.setLeft,
tree.setRight,
tree.setRoot,
tree.getRoot, or insert into a BST with
tree.insertBST(tree, value) (values must be comparable).
Traversals return ordinary SPL lists you can pass to
print.list:
tree.preorder,
tree.inorder,
tree.postorder,
tree.levelOrder (breadth-first).
See examples/builtin_ds.spl for stack, queue, linked list, heap, and tree.
Deque and priority queue follow the same create → push → pop/peek pattern:
deque.createDeque(D); deque.pushBack(D, 1); deque.pushFront(D, 0); print.number(deque.peekFront(D)); priorityQueue.createPriorityQueue(PQ); priorityQueue.push(PQ, 2, "low"); priorityQueue.push(PQ, 1, "high"); print.string(priorityQueue.pop(PQ));