← SPL reference

Advanced data structures

Linked lists, heaps, stacks, queues, and trees

SPL provides built-in mutable containers under the namespaces stack, queue, 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.isLinkedList, type.isHeap, type.isTree, and type.isTreeNode to branch at runtime. Typed printing and returns use print.stackprint.treeNode and return.stackreturn.treeNode.

Stack

LIFO: stack.createStack(Name); then stack.push, stack.pop, stack.peek, stack.size, stack.isEmpty.

Queue

FIFO: queue.createQueue(Name); then queue.enqueue, queue.dequeue, queue.peekFront, queue.size, queue.isEmpty.

Linked list

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.

Heap

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.

Binary tree

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).

Example

See examples/builtin_ds.spl in the repository for a short script that exercises each structure.