π 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.
πΆ 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.
π» 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++];
}