Stacks and Queues

Back

Loading concept...

๐Ÿ“š 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:

  1. Stacks = Last In, First Out (like pancakes!)
  2. 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! ๐Ÿš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.