๐ Stacks and Queues: The Story of Two Lines
๐ญ The Tale of Two Worlds
Imagine youโre at a busy restaurant. In the kitchen, thereโs a stack of plates. The cook puts clean plates on top, and when the waiter needs one, they take from the top too. Thatโs a Stack!
Now look at the customers waiting outside. The first person who arrived gets seated first. New people join at the back. Thatโs a Queue!
These two simple ideas power everything from your browserโs โBackโ button to how your phone handles messages!
๐ฅ Chapter 1: The Stack โ A Pile of Pancakes
What is a Stack?
Think of a stack of pancakes on your plate:
- You add new pancakes on top
- You eat from the top first
- You canโt grab a pancake from the middle!
This is called LIFO: Last In, First Out
The last pancake added is the first one you eat!
graph TD A["๐ฅ Pancake 3 - TOP"] --> B["๐ฅ Pancake 2"] B --> C["๐ฅ Pancake 1 - BOTTOM"] style A fill:#FFD700 style B fill:#FFA500 style C fill:#FF6B6B
Real Life Examples
- ๐ A stack of books on your desk
- ๐ฝ๏ธ Plates in a cafeteria
- โฉ๏ธ The โUndoโ button in apps
- ๐ Your browserโs โBackโ button
๐ฏ Chapter 2: Stack Operations
A stack has four magical powers:
1. PUSH โ Add to the Top! ๐ผ
// Adding an item to stack
stack[++top] = newItem;
Like putting a new book on top of your pile!
2. POP โ Remove from Top! ๐ฝ
// Removing top item
item = stack[top--];
Like taking the top book off your pile!
3. PEEK (or TOP) โ Look Without Touching! ๐
// Just looking at top item
topItem = stack[top];
Like checking which book is on top without moving it!
4. isEmpty โ Is the Stack Empty? ๐ค
// Check if stack is empty
if (top == -1) {
// Stack is empty!
}
Like checking if your plate has any pancakes left!
๐๏ธ Building a Stack in C
Hereโs a simple stack in C:
#define MAX 100
int stack[MAX];
int top = -1;
void push(int x) {
if (top < MAX - 1) {
stack[++top] = x;
}
}
int pop() {
if (top >= 0) {
return stack[top--];
}
return -1;
}
int peek() {
if (top >= 0) {
return stack[top];
}
return -1;
}
int isEmpty() {
return top == -1;
}
Watch It Work!
graph LR A["push 10"] --> B["[10]"] B --> C["push 20"] C --> D["[10,20]"] D --> E["push 30"] E --> F["[10,20,30]"] F --> G["pop"] G --> H["[10,20]"]
๐ถ Chapter 3: The Queue โ A Line at the Movies
What is a Queue?
Picture the line at a movie theater:
- New people join at the back
- People enter the theater from the front
- No cutting in line!
This is called FIFO: First In, First Out
The first person to arrive is the first to be served!
graph LR A["๐ง Person 1 - FRONT"] --> B["๐ง Person 2"] B --> C["๐ง Person 3"] C --> D["๐ง Person 4 - REAR"] style A fill:#4ECDC4 style D fill:#FF6B6B
Real Life Examples
- ๐ฌ Line at movie theater
- ๐จ๏ธ Print jobs waiting to print
- ๐ฑ Messages waiting to be sent
- ๐ฎ Players waiting to join a game
๐ฏ Chapter 4: Queue Operations
A queue has four super powers:
1. ENQUEUE โ Join the Line! โก๏ธ
// Adding to rear of queue
queue[++rear] = newItem;
Like joining the back of a line!
2. DEQUEUE โ Leave from Front! โฌ ๏ธ
// Removing from front
item = queue[front++];
Like the first person being called forward!
3. FRONT (or PEEK) โ Whoโs First? ๐
// Looking at front person
firstItem = queue[front];
Like seeing whoโs at the front without moving them!
4. isEmpty โ Is the Line Empty? ๐ค
// Check if queue is empty
if (front > rear) {
// Queue is empty!
}
Like checking if anyone is still waiting!
๐๏ธ Building a Queue in C
Hereโs a simple queue in C:
#define MAX 100
int queue[MAX];
int front = 0;
int rear = -1;
void enqueue(int x) {
if (rear < MAX - 1) {
queue[++rear] = x;
}
}
int dequeue() {
if (front <= rear) {
return queue[front++];
}
return -1;
}
int peek() {
if (front <= rear) {
return queue[front];
}
return -1;
}
int isEmpty() {
return front > rear;
}
Watch It Work!
graph LR A["enqueue 10"] --> B["[10]"] B --> C["enqueue 20"] C --> D["[10,20]"] D --> E["enqueue 30"] E --> F["[10,20,30]"] F --> G["dequeue"] G --> H["[20,30]"]
โ๏ธ Stack vs Queue: The Ultimate Showdown
| Feature | Stack ๐ฅ | Queue ๐ถ |
|---|---|---|
| Order | LIFO | FIFO |
| Add | Top (Push) | Rear (Enqueue) |
| Remove | Top (Pop) | Front (Dequeue) |
| Analogy | Pancakes | Movie Line |
| Uses | Undo, Back | Printing, Messages |
๐ Why Do These Matter?
Stacks Power:
- โฉ๏ธ Undo/Redo in apps
- ๐งฎ Calculators - solving math expressions
- ๐ Browser history - going back to previous pages
- ๐ Function calls - how programs remember where to return
Queues Power:
- ๐จ๏ธ Print queues - documents waiting to print
- ๐ฌ Message systems - texts waiting to send
- ๐ฎ Game lobbies - players waiting for matches
- ๐ Web servers - handling requests in order
๐ You Did It!
You just learned two of the most powerful ideas in programming:
- Stacks = Last In, First Out (like pancakes!)
- Queues = First In, First Out (like a movie line!)
These simple concepts are the building blocks of apps, games, and websites you use every day!
๐ก Remember:
- Stack = Pancakes (top to top)
- Queue = Line (back to front)
Now youโre ready to build amazing things! ๐
