Stack
Stacks are Last-In-First-Out (LIFO) data structures where elements are added and removed from the top. They are perfect for visualizing function calls, expression evaluation, and backtracking algorithms.
Source Code
1stack callStack = {2 value: ["main", "process", "calculate", "validate"]3 color: [null, "blue", "red", "green"]4 arrow: [null, null, null, "top"]5}6
7page8show callStackRendered Diagram
Properties
Stacks support the following properties:
| Property | Type | Description |
|---|---|---|
value | (number | string | null)[] | Stack elements (bottom to top) |
color | (color | null)[] | Colors for each element |
arrow | (number | string | null)[] | Arrows or labels for each element |
above | string | text_object (see special features) | Label positioned above the stack |
below | string | text_object | Label positioned below the stack |
left | string | text_object | Label positioned to the left |
right | string | text_object | Label positioned to the right |
Methods
Stacks support these methods for manipulation:
Text Positioning Methods
setText(text, position)- Set or remove text at specific positions ("above","below","left","right")
Single Element Methods
setValue(index, value)- Set value at specific positionsetColor(index, color)- Set color at specific positionsetArrow(index, arrow)- Set arrow at specific position
Multiple Element Methods
setValues([...])- Set multiple values (use_to keep existing)setColors([...])- Set multiple colorssetArrows([...])- Set multiple arrows
Stack Operations
addValue(value)- Push value onto top of stackinsertValue(index, value)- Insert value at specific positionremoveValue(value)- Remove first occurrence of valueremoveAt(index)- Remove element at specific position
Examples
Basic Stack Example
Function Call Stack
Source Code
1stack functions = {2 value: ["main()", "fact(5)", "fact(4)", "fact(3)"]3 color: ["green", "blue", "yellow", "red"]4 arrow: [null, null, null, "current"]5 below: "Call Stack"6}7
8page9show functionsRendered Diagram
Stack Push/Pop Operations
Source Code
1stack numbers = {2 value: [10, 20]3 color: [null, null]4}5
6page7show numbers8
9page10numbers.addValue(30)11numbers.setColor(2, "blue")12numbers.setArrow(2, "pushed")13
14page15numbers.addValue(40)16numbers.setColor(3, "green")17numbers.setArrow(3, "top")18numbers.setArrow(2, null)19
20page21numbers.removeAt(3)22numbers.setArrow(2, "top")Rendered Diagram
Expression Evaluation Stack
Source Code
1stack operators = {2 value: ["+", "*", "("]3 color: ["green", "blue", "red"]4 arrow: [null, null, "latest"]5 above: "Operator Stack"6 below: "Expression: 3 + 4 * (2 + 1)"7}8
9page10show operatorsRendered Diagram
Undo/Redo Stack
Source Code
1stack undoStack = {2 value: ["'Hello'", "del char", "' World'"]3 color: ["gray", "gray", "blue"]4 arrow: [null, null, "current"]5 above: "Undo History"6}7
8page9show undoStackRendered Diagram
Memory Stack Visualization
Source Code
1stack memory = {2 value: ["int x = 5", "ch c = 'A'", "fl pi = 3.1"]3 color: ["lightblue", "lightgreen", "lightyellow"]4 arrow: [null, null, "stack pointer"]5 left: "Stack Frame"6 below: "Memory grows upward"7}8
9page10show memoryRendered Diagram
Use Cases
Stacks are ideal for:
- Function call management - Call stack, recursion visualization
- Expression evaluation - Infix to postfix, parentheses matching
- Backtracking algorithms - Maze solving, N-Queens problem
- Undo/Redo operations - Text editors, action history
- Browser history - Back/forward navigation
- Memory management - Stack frames, local variables
- Parsing - Syntax analysis, compiler design
- DFS algorithms - Depth-first search implementation
Related
- Methods Reference - Complete list of stack methods
- Array - Similar linear structure with indexing
- LinkedList - Alternative linear structure