A simple terminal simulator for learning stack, queue, and recursion concepts.
├── frontend/ # Frontend files
│ ├── index.html # Main HTML page
│ ├── style.css # Styling
│ └── app.js # Main application logic
└── operations/ # Data structure operations
├── stack.js # Stack implementation
├── queue.js # Queue implementation
└── recursion.js # Recursion functions
- Open
frontend/index.htmlin your web browser - Type commands in the terminal
push <value>- Add item to stack (3 clock cycles)pop- Remove item from stack (2 clock cycles)peek- View top item (1 clock cycle)show stack- Display all stack items
enqueue <value>- Add item to queue (3 clock cycles)dequeue- Remove item from queue (2 clock cycles)front- View front item (1 clock cycle)show queue- Display all queue items
fibonacci <n>- Calculate fibonacci (pure recursion - exponential)fibonacci <n> dp- Fibonacci with memoization (top-down DP)fibonacci <n> iter- Fibonacci iteratively (O(n) time, O(1) space)fibonacci <n> tab- Fibonacci with tabulation (bottom-up DP)factorial <n>- Calculate factorial (shows recursive calls)
stats- Show system statisticsmemoryormem- Show memory map with addressesreset- Reset clock cyclesclear- Clear the screenhelp- Show help message
The terminal displays:
- Clock Cycles: Total CPU cycles consumed
- Stack Size: Number of items in stack
- Queue Size: Number of items in queue
- Each stack/queue item gets a unique memory address (e.g., 0x3A5F2B)
- Memory addresses shown when pushing/enqueueing
- Memory freed notification when popping/dequeueing
showcommands display values with their addressesmemorycommand shows complete memory map
- Step-by-step execution with time delays
- Shows what's happening during each operation
- Memory allocation, data operations, and cleanup are visualized
- Makes the learning process more intuitive
Each operation consumes clock cycles:
- Push/Enqueue: 3 cycles
- Pop/Dequeue: 2 cycles
- Peek/Front: 1 cycle
- Fibonacci (recursive): 3 cycles per call
- Fibonacci (DP/memoization): 2 cycles per call
- Fibonacci (iterative/tabulation): 1 cycle per operation
- Factorial: 2 cycles per call
Compare different Fibonacci approaches:
- Recursive (
fibonacci 20) - Exponential time O(2^n) - DP Memoization (
fibonacci 20 dp) - Linear time O(n), uses recursion - Iterative (
fibonacci 20 iter) - Linear time O(n), no recursion, O(1) space - Tabulation (
fibonacci 20 tab) - Linear time O(n), bottom-up DP with array
$ push apple
# Allocating memory...
# Memory allocated at 0x3A5F2B
# Pushing "apple" to stack...
# Pushed "apple" to stack
# Clock cycles: +3
$ show stack # Shows items with memory addresses
$ pop
# Accessing top of stack...
# Removing "apple"...
# Freeing memory at 0x3A5F2B...
# Popped "apple" from stack
# Clock cycles: +2
$ enqueue task1
$ enqueue task2
$ dequeue
$ memory # View complete memory map
$ fibonacci 10
$ fibonacci 10 dp
$ factorial 5
$ stats
$ reset
Try these to see the difference between different Fibonacci approaches:
$ fibonacci 20 # Recursive (slowest, many calls)
$ fibonacci 20 dp # Memoization (top-down DP)
$ fibonacci 20 iter # Iterative (fastest, minimal space)
$ fibonacci 20 tab # Tabulation (bottom-up DP)
$ stats # Check clock cycles used
Example output comparison for fibonacci 15:
- Recursive: ~1,500+ clock cycles
- DP: ~30 clock cycles
- Iterative: ~13 clock cycles
- Tabulation: ~15 clock cycles
The stack uses a Last-In-First-Out (LIFO) structure:
push()adds items to the end of an arraypop()removes items from the end- Each item stores both its value and memory address
- Time complexity: O(1) for all operations
The queue uses a First-In-First-Out (FIFO) structure:
enqueue()adds items to the end of an arraydequeue()removes items from the beginning usingshift()- Each item stores both its value and memory address
- Time complexity: O(1) for enqueue, O(n) for dequeue
Memory addresses are randomly generated hexadecimal values:
- Format:
0xfollowed by 6 hex digits (e.g., 0x3A5F2B) - Generated using:
Math.random() * 0xFFFFFF - Each push/enqueue creates a new address
- Addresses are displayed when items are added or removed
Operations consume cycles to simulate CPU time:
- Stack/Queue Operations: 3 cycles (allocation + operation)
- Peek/Front: 1 cycle (read-only)
- Recursive Fibonacci: 3 cycles per call (simulates heavy computation)
- DP/Memoized: 2 cycles per call (faster due to caching)
- Iterative: 1 cycle per iteration (most efficient)
The terminal uses variable delays based on input size:
| Input Size | Delay Multiplier | Example (700ms base) |
|---|---|---|
| n ≤ 10 | 100% (1.0) | 700ms |
| 11-20 | 70% (0.7) | 490ms |
| 21-30 | 50% (0.5) | 350ms |
| 31-50 | 30% (0.3) | 210ms |
| 50+ | 10% (0.1) | 70ms |
This ensures small numbers show educational steps while large numbers execute quickly.
1. Recursive (Exponential O(2^n))
fib(5)
├── fib(4)
│ ├── fib(3)
│ │ ├── fib(2)
│ │ └── fib(1)
│ └── fib(2)
└── fib(3)
├── fib(2)
└── fib(1)Many redundant calculations. Each call spawns two more calls.
2. Memoization (Top-Down O(n))
memo = {}
fib(5) → calculates and stores
fib(4) → calculates and stores
fib(3) → retrieves from memoCaches results to avoid recalculation. Uses recursion with memory.
3. Iterative (O(n) with O(1) space)
a = 0, b = 1
Loop: temp = a + b, a = b, b = tempNo recursion. Just loops with two variables.
4. Tabulation (Bottom-Up O(n))
table[0] = 0, table[1] = 1
for i = 2 to n:
table[i] = table[i-1] + table[i-2]Builds solution from bottom. Uses an array.
Time Complexity:
- Recursive: Terrible for large n (grows exponentially)
- Others: All linear O(n), manageable for large n
Space Complexity:
- Iterative: Best (constant space)
- Others: Linear space (stack or array)
Use Cases:
- Recursive: Teaching, small inputs
- DP Memoization: When you need recursion but want speed
- Iterative: Production code, best performance
- Tabulation: When you need all intermediate values
Frontend Folder:
- Keeps UI separate from logic
- Makes it easy to swap different UIs
Operations Folder:
- Each data structure in its own file
- Easy to test and modify individually
- Can be reused in other projects
No Build Tools:
- Simple to run (just open HTML)
- No npm, webpack, or compilation needed
- Great for learning and quick demos
This project is an interactive educational tool designed to help students understand fundamental data structures (Stack and Queue) and algorithm optimization techniques through visual simulation. The terminal-based interface provides real-time feedback on operations, memory management, and computational complexity.
Students often struggle to understand:
- How data structures work internally
- The performance difference between algorithmic approaches
- Memory allocation and deallocation in programs
- The real cost (clock cycles) of different operations
Traditional teaching methods use static diagrams and text that don't convey the dynamic nature of these concepts.
To create an interactive terminal simulator that:
- Demonstrates Stack (LIFO) and Queue (FIFO) operations in real-time
- Shows memory address allocation and management
- Compares recursive vs optimized algorithms (Fibonacci implementations)
- Tracks computational cost through clock cycle simulation
- Provides immediate visual feedback for learning
Educational Impact:
- Makes abstract computer science concepts tangible and visual
- Helps students understand why algorithm optimization matters
- Bridges the gap between theory and implementation
- Free and accessible learning tool for anyone with a web browser
Real-world Relevance:
- Stack/Queue are fundamental to OS scheduling, browser history, undo systems
- Algorithm optimization (DP) is critical in AI, game development, and data processing
- Memory management concepts apply to system programming and embedded systems
- Performance analysis skills are essential for professional software development
1. Stack (LIFO - Last In First Out)
- Implementation: Array-based with object storage
- Operations: push, pop, peek
- Applications: Function call stack, expression evaluation, undo mechanisms
- Time Complexity: O(1) for all operations
2. Queue (FIFO - First In First Out)
- Implementation: Array-based with object storage
- Operations: enqueue, dequeue, front
- Applications: Task scheduling, breadth-first search, print spoolers
- Time Complexity: O(1) for enqueue, O(n) for dequeue
3. Hash Table (Implicit in Memoization)
- Used in Fibonacci DP for caching computed values
- Key-value storage for O(1) lookup
- Demonstrates space-time tradeoff
Architecture:
Frontend (UI Layer)
├── HTML - Structure
├── CSS - Styling
└── JavaScript - User interaction
Operations (Logic Layer)
├── Stack.js - Stack implementation
├── Queue.js - Queue implementation
└── Recursion.js - Algorithm implementations
Key Features Implemented:
-
Memory Simulation
- Random hexadecimal address generation
- Address tracking for each data element
- Visual representation of allocation/deallocation
-
Clock Cycle Tracking
- Different cycle costs for different operations
- Real-time display of computational cost
- Helps understand algorithm efficiency
-
Adaptive Animation System
- Variable delays based on input size
- Prevents long waits for large inputs
- Maintains educational value for small inputs
-
Multiple Algorithm Implementations
- Recursive Fibonacci (exponential complexity)
- Memoization (top-down DP)
- Iterative (space-optimized)
- Tabulation (bottom-up DP)
Technology Stack:
- Pure HTML, CSS, JavaScript (no frameworks)
- Modular architecture for maintainability
- Asynchronous operations for smooth animations
Core Algorithms:
// Recursive Fibonacci - O(2^n)
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
// Memoized Fibonacci - O(n)
function fibonacciDP(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fibonacciDP(n-1, memo) + fibonacciDP(n-2, memo);
return memo[n];
}
// Iterative Fibonacci - O(n) time, O(1) space
function fibonacciIterative(n) {
if (n <= 1) return n;
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
}Performance Metrics:
- Fibonacci(20) Recursive: ~21,891 calls, ~65,673 cycles
- Fibonacci(20) DP: ~21 calls, ~42 cycles
- Fibonacci(20) Iterative: 19 iterations, ~19 cycles
- Successfully demonstrates Stack and Queue operations with visual feedback
- Shows dramatic performance difference between algorithms (1500x improvement)
- Memory addresses provide concrete understanding of data storage
- Adaptive delays make both small and large inputs practical
- Zero installation required - runs in any modern browser
User Feedback:
- Intuitive interface with terminal aesthetic
- Real-time statistics help understand performance
- Step-by-step execution aids learning
- Command history (arrow keys) improves usability
This project successfully creates an interactive learning environment for data structures and algorithms. By combining visual feedback, performance metrics, and multiple implementation approaches, it addresses the gap between theoretical knowledge and practical understanding. The terminal simulator makes abstract concepts concrete and measurable.
Key Achievements:
- Implemented fundamental data structures from scratch
- Demonstrated importance of algorithm optimization
- Created educational tool with real-world applicability
- Zero-dependency, browser-based solution
Potential Enhancements:
-
Additional Data Structures
- Binary Trees (traversal animations)
- Graphs (BFS/DFS visualization)
- Hash Tables (collision handling)
- Heaps (priority queue operations)
-
Advanced Features
- Step-by-step algorithm visualization
- Code highlighting during execution
- Export operation history to file
- Comparison mode (run two algorithms side-by-side)
-
Educational Additions
- Quiz mode to test understanding
- Guided tutorials for each data structure
- Code challenges with automated testing
- Performance benchmarking tools
-
Technical Improvements
- Custom themes (dark/light mode)
- Keyboard shortcuts for common operations
- Save/load state functionality
- Mobile-responsive design
-
Integration Possibilities
- LMS (Learning Management System) integration
- Progress tracking and analytics
- Multi-user collaborative sessions
- REST API for programmatic access