Skip to main content

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
7page
8show callStack

Rendered Diagram

Properties

Stacks support the following properties:

PropertyTypeDescription
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
abovestring | text_object (see special features)Label positioned above the stack
belowstring | text_objectLabel positioned below the stack
leftstring | text_objectLabel positioned to the left
rightstring | text_objectLabel 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 position
  • setColor(index, color) - Set color at specific position
  • setArrow(index, arrow) - Set arrow at specific position

Multiple Element Methods

  • setValues([...]) - Set multiple values (use _ to keep existing)
  • setColors([...]) - Set multiple colors
  • setArrows([...]) - Set multiple arrows

Stack Operations

  • addValue(value) - Push value onto top of stack
  • insertValue(index, value) - Insert value at specific position
  • removeValue(value) - Remove first occurrence of value
  • removeAt(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
8page
9show functions

Rendered Diagram

Stack Push/Pop Operations

Source Code

1stack numbers = {
2 value: [10, 20]
3 color: [null, null]
4}
5
6page
7show numbers
8
9page
10numbers.addValue(30)
11numbers.setColor(2, "blue")
12numbers.setArrow(2, "pushed")
13
14page
15numbers.addValue(40)
16numbers.setColor(3, "green")
17numbers.setArrow(3, "top")
18numbers.setArrow(2, null)
19
20page
21numbers.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
9page
10show operators

Rendered 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
8page
9show undoStack

Rendered 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
9page
10show memory

Rendered 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