Skip to main content
C Programming Basics
CHAPTER 23 Beginner

Stack and Queue Implementation

Updated: May 17, 2026
5 min read

# CHAPTER 23

Stack and Queue Implementation

1. Introduction

Stacks and Queues are abstract data structures that dictate *how* data is added and removed. A Stack works like a stack of plates at a buffet. A Queue works like a line of people at a movie theater. In this chapter, we will implement both using Arrays in C.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain the LIFO principle of a Stack.
  • Implement Stack operations: push, pop, peek.
  • Explain the FIFO principle of a Queue.
  • Implement Queue operations: enqueue, dequeue.
  • Understand Overflow and Underflow conditions.

3. Stack (LIFO - Last In, First Out)

The last item placed on the stack is the first one removed.
  • Push: Add an item to the top.
  • Pop: Remove an item from the top.
  • Peek/Top: Look at the top item without removing it.
c
1234567891011121314151617181920212223242526272829303132333435
#include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1; // -1 means the stack is empty

// Add item to stack
void push(int value) {
    if (top == MAX - 1) {
        printf("Stack Overflow! Cannot push %d\n", value);
    } else {
        top++; // Move top up
        stack[top] = value;
        printf("Pushed %d\n", value);
    }
}

// Remove item from stack
void pop() {
    if (top == -1) {
        printf("Stack Underflow! Stack is empty.\n");
    } else {
        printf("Popped %d\n", stack[top]);
        top--; // Move top down
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    pop(); // Removes 30
    pop(); // Removes 20
    return 0;
}

4. Visualizing a Stack

123456
push(10)   push(20)   push(30)     pop()

           |      |   |  30  | <-top
|      |   |  20  |   |  20  |   |  20  | <-top
|  10  |   |  10  |   |  10  |   |  10  |
+------+   +------+   +------+   +------+

5. Queue (FIFO - First In, First Out)

The first item placed in the queue is the first one removed.
  • Enqueue: Add an item to the rear (back).
  • Dequeue: Remove an item from the front.
c
1234567891011121314151617181920212223242526272829303132333435
#include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1;
int rear = -1;

void enqueue(int value) {
    if (rear == MAX - 1) {
        printf("Queue is Full!\n");
    } else {
        if (front == -1) front = 0; // Set front on first insert
        rear++;
        queue[rear] = value;
        printf("Enqueued %d\n", value);
    }
}

void dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue is Empty!\n");
    } else {
        printf("Dequeued %d\n", queue[front]);
        front++; // Move front forward
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    dequeue(); // Removes 10
    dequeue(); // Removes 20
    return 0;
}

6. Visualizing a Queue

12345678
enqueue(10, 20, 30)

Front             Rear
  |                 |
  v                 v
[ 10 ][ 20 ][ 30 ][  ][  ]

dequeue() -> Removes 10, Front moves to 20.

*(Note: A basic array queue wastes space as front moves forward. A "Circular Queue" solves this by wrapping around).*

7. Real-World Applications

  • Stacks: The Call Stack (functions), Undo features in text editors, Browser back buttons, balancing parentheses in compilers.
  • Queues: Printer job scheduling, CPU task scheduling, waiting lines in web servers, BFS (Breadth-First Search) algorithms.

8. Common Mistakes

  • Stack Overflow: Trying to push when top == MAX - 1.
  • Stack Underflow: Trying to pop when top == -1.
  • Queue Memory Waste: In a linear array queue, dequeuing doesn't shift elements, so the front spaces become unusable. (Fix: Use a Circular Queue or Linked List).

9. Exercises

  1. 1. Add a peek() function to the Stack code that returns the top element without popping it.
  1. 2. Add a display() function to the Queue code that prints all current elements from front to rear.

10. MCQ Quiz with Answers

Question 1

A Stack follows which principle?

Question 2

A Queue follows which principle?

Question 3

What is it called when you try to Pop from an empty stack?

Question 4

Where are elements added in a Queue?

Question 5

Where are elements removed from in a Queue?

Question 6

What does top start at in an empty array-based stack?

Question 7

Which operation looks at the top of the stack without removing it?

Question 8

The Undo function in Microsoft Word uses which data structure?

Question 9

Print jobs sent to a printer use which data structure?

Question 10

What is a limitation of a simple array-based Queue?

11. Interview Questions

  • Q: How would you implement a Queue using two Stacks?
  • Q: What is a Circular Queue, and how does it solve the memory waste problem of a linear array queue?
  • Q: Explain how the OS uses a Stack for function calls.

12. Summary

Stacks and Queues are logical structures built on top of Arrays (or Linked Lists). Stacks are LIFO (push/pop at the top) and are used for reversing, undoing, and recursion. Queues are FIFO (enqueue at rear, dequeue at front) and are used for scheduling and sequential processing.

13. Next Chapter Recommendation

In Chapter 24: Searching and Sorting Algorithms, we will learn how to find data and organize data sequentially using Linear Search, Binary Search, and Bubble Sort.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·