πŸ“š Module 3A: Stacks (LIFO Data Structure)

Understand the Last-In, First-Out (LIFO) mechanism, real-world analogies, operation complexities, and C function call stack frames.

πŸ’‘ 1. What is a Stack?

A Stack is a linear Abstract Data Type (ADT) operating on the LIFO (Last In, First Out) principle. Insertion and deletion occur strictly at a single open endpoint called the TOP of the stack.

πŸ”‘ Key Rule: LIFO Access

The element inserted last is always the very first element to be removed. You cannot access lower elements without first popping the elements above them!

🎯 Primary Entry Point: TOP Pointer

All operations (push, pop, peek) are performed at the Top pointer in constant O(1) time complexity.

🌎 Real-World & System Analogies of Stacks

🍽️

A. Stack of Plates

Plates placed in a cafeteria stack. Added to top (Push), washed from top (Pop).

🌐

B. Browser Back Button

Every URL visited is pushed onto history stack. Clicking Back pops current URL.

↩️

C. Text Editor Undo (Ctrl+Z)

Modifications stored in Undo Stack. Pressing Ctrl+Z pops recent edit action.

βš™οΈ

D. C Function Call Stack

CPU reserves Stack Frames for function calls. Returning pops activation records.

πŸ“š Interactive Stack Simulator (LIFO)

Push, Pop, and Peek operations with step-by-step playback, variable watch, and line-by-line execution tracker.

Click ⬆️ push(val) or ⬇️ pop() above to step manually.
Ready
⚑ GCC C Line Execution Tracker
No active variables

🚢 Module 3B: Queues (FIFO Data Structure)

Master the First-In, First-Out (FIFO) principle, Front & Rear pointers, linear wasted space, circular queues, and dynamic linked list queues.

πŸ’‘ 1. What is a Queue?

A Queue is a linear Abstract Data Type (ADT) operating on the FIFO (First In, First Out) principle. Elements enter at the REAR pointer and exit at the FRONT pointer.

πŸ”‘ Key Rule: FIFO Access

The element inserted first is always the very first element to be served and removed. Fair service order!

🎯 Dual Pointers: FRONT & REAR

enqueue() adds at Rear. dequeue() removes from Front. Both operate in O(1) time complexity.

🌎 Real-Life Applications of Queues

🎟️

A. Ticket Counter Line

First person arriving at ticket window is served first.

πŸ–¨οΈ

B. Printer Print Spooler

Print jobs printed in exact arrival order.

🏦

C. Bank Teller Counter

Bank customers waiting in line for next teller window.

βš™οΈ

D. CPU Process Scheduler

OS Ready Queue allocates CPU execution time slots.

🚢 Interactive Queue Simulator (FIFO)

Simulate Linear Array Queue, Circular Ring Queue, and Dynamic Linked List Queue.

Click βž• enqueue(val) or ❌ dequeue() above to step manually.
Ready
⚑ GCC C Line Execution Tracker
No active variables

πŸ’» Essential C Programs for Stacks & Queues

Production-ready C source code for university exams and coding interviews.

1. Array Implementation of Stack in C

#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;

void push(int val) {
    if (top == MAX - 1) return;
    stack[++top] = val;
}
int pop() {
    if (top == -1) return -1;
    return stack[top--];
}

2. Linear Array Queue Implementation in C

#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;

void enqueue(int val) {
    if (rear == MAX - 1) return;
    if (front == -1) front = 0;
    queue[++rear] = val;
}
int dequeue() {
    if (front == -1 || front > rear) return -1;
    return queue[front++];
}