Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data Structures Terminal Simulator

A simple terminal simulator for learning stack, queue, and recursion concepts.

Project Structure

├── 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

How to Run

  1. Open frontend/index.html in your web browser
  2. Type commands in the terminal

Available Commands

Stack Operations

  • 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

Queue Operations

  • 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

Recursion & Algorithms

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

System

  • stats - Show system statistics
  • memory or mem - Show memory map with addresses
  • reset - Reset clock cycles
  • clear - Clear the screen
  • help - Show help message

Features

Real-time Statistics

The terminal displays:

  • Clock Cycles: Total CPU cycles consumed
  • Stack Size: Number of items in stack
  • Queue Size: Number of items in queue

Memory Management

  • Each stack/queue item gets a unique memory address (e.g., 0x3A5F2B)
  • Memory addresses shown when pushing/enqueueing
  • Memory freed notification when popping/dequeueing
  • show commands display values with their addresses
  • memory command shows complete memory map

Animated Operations

  • 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

Clock Cycle Tracking

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

Algorithm Comparison

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

Examples

Basic Operations

$ 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

Queue Operations

$ enqueue task1
$ enqueue task2
$ dequeue
$ memory                  # View complete memory map

Algorithm Analysis

$ fibonacci 10
$ fibonacci 10 dp
$ factorial 5
$ stats
$ reset

Performance Comparison

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

Technical Details

How It Works

Stack Implementation

The stack uses a Last-In-First-Out (LIFO) structure:

  • push() adds items to the end of an array
  • pop() removes items from the end
  • Each item stores both its value and memory address
  • Time complexity: O(1) for all operations

Queue Implementation

The queue uses a First-In-First-Out (FIFO) structure:

  • enqueue() adds items to the end of an array
  • dequeue() removes items from the beginning using shift()
  • Each item stores both its value and memory address
  • Time complexity: O(1) for enqueue, O(n) for dequeue

Memory Simulation

Memory addresses are randomly generated hexadecimal values:

  • Format: 0x followed 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

Clock Cycle System

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)

Adaptive Delay System

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.

Fibonacci Algorithms Explained

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 memo

Caches 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 = temp

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

Why Different Approaches Matter

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

Project Structure Rationale

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

Academic Project Documentation

Introduction

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.

Problem Statement

Students often struggle to understand:

  1. How data structures work internally
  2. The performance difference between algorithmic approaches
  3. Memory allocation and deallocation in programs
  4. 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.

Objective

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

Relevance & Societal Impact

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

Data Structures Used

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

Approach & Methodology

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:

  1. Memory Simulation

    • Random hexadecimal address generation
    • Address tracking for each data element
    • Visual representation of allocation/deallocation
  2. Clock Cycle Tracking

    • Different cycle costs for different operations
    • Real-time display of computational cost
    • Helps understand algorithm efficiency
  3. Adaptive Animation System

    • Variable delays based on input size
    • Prevents long waits for large inputs
    • Maintains educational value for small inputs
  4. Multiple Algorithm Implementations

    • Recursive Fibonacci (exponential complexity)
    • Memoization (top-down DP)
    • Iterative (space-optimized)
    • Tabulation (bottom-up DP)

Solution & Implementation

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

Results & Achievements

  1. Successfully demonstrates Stack and Queue operations with visual feedback
  2. Shows dramatic performance difference between algorithms (1500x improvement)
  3. Memory addresses provide concrete understanding of data storage
  4. Adaptive delays make both small and large inputs practical
  5. 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

Conclusion

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

Future Scope

Potential Enhancements:

  1. Additional Data Structures

    • Binary Trees (traversal animations)
    • Graphs (BFS/DFS visualization)
    • Hash Tables (collision handling)
    • Heaps (priority queue operations)
  2. Advanced Features

    • Step-by-step algorithm visualization
    • Code highlighting during execution
    • Export operation history to file
    • Comparison mode (run two algorithms side-by-side)
  3. Educational Additions

    • Quiz mode to test understanding
    • Guided tutorials for each data structure
    • Code challenges with automated testing
    • Performance benchmarking tools
  4. Technical Improvements

    • Custom themes (dark/light mode)
    • Keyboard shortcuts for common operations
    • Save/load state functionality
    • Mobile-responsive design
  5. Integration Possibilities

    • LMS (Learning Management System) integration
    • Progress tracking and analytics
    • Multi-user collaborative sessions
    • REST API for programmatic access

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages