C Programming
Stacks and Queues
March 24, 2026
C Programming March 24, 2026 1 / 14
Use of Stack
A stack is used when data must be accessed in reverse order of
insertion.
The last inserted item is removed first.
Common uses of stack:
Function call management in programs
Undo operation in editors
Expression evaluation
Parenthesis matching
Example idea: stack of plates
C Programming March 24, 2026 2 / 14
Use of Queue
A queue is used when data must be processed in the same order as
insertion.
The first inserted item is removed first.
Common uses of queue:
Printer queue
CPU scheduling
Waiting line systems
Data buffering
Example idea: people standing in a line
C Programming March 24, 2026 3 / 14
LIFO and FIFO
LIFO = Last In First Out
Last inserted element is removed first
Used in stack
FIFO = First In First Out
First inserted element is removed first
Used in queue
So:
Stack follows LIFO
Queue follows FIFO
C Programming March 24, 2026 4 / 14
Stack Using Array
Stack can be implemented using an array.
One variable called top keeps track of the topmost element.
Initially, stack is empty, so:
top = -1
Main operations:
push
pop
peek / top
C Programming March 24, 2026 5 / 14
Stack Declaration in C
1 #include <stdio.h>
2 #define MAX 5
3
4 int stack[MAX];
5 int top = -1;
6
C Programming March 24, 2026 6 / 14
Push Operation in Stack
1 void push(int x) {
2 if (top == MAX - 1) {
3 printf("Stack Overflow\n");
4 } else {
5 top++;
6 stack[top] = x;
7 printf("%d inserted into stack\n", x);
8 }
9 }
0
C Programming March 24, 2026 7 / 14
Pop Operation in Stack
1 int pop() {
2 if (top == -1) {
3 printf("Stack Underflow\n");
4 return -1;
5 } else {
6 return stack[top--];
7 }
8 }
9
C Programming March 24, 2026 8 / 14
Top / Peek Operation in Stack
1 int peek() {
2 if (top == -1) {
3 printf("Stack is Empty\n");
4 return -1;
5 } else {
6 return stack[top];
7 }
8 }
9
C Programming March 24, 2026 9 / 14
Complete Stack Program
1 #include <stdio.h>
2 #define MAX 5
3
4 int stack[MAX];
5 int top = -1;
6
7 void push(int x) {
8 if (top == MAX - 1)
9 printf("Stack Overflow\n");
0 else
1 stack[++top] = x;
2 }
3
4 int pop() {
5 if (top == -1) {
6 printf("Stack Underflow\n");
7 return -1;
8 }
9 return stack[top--];
0 }
1
C Programming March 24, 2026 10 / 14
Queue Using Array
Queue can also be implemented using an array.
Two variables are used:
front for deletion
rear for insertion
Initially:
front = -1
rear = -1
Main operations:
enqueue
dequeue
peek
C Programming March 24, 2026 11 / 14
Queue Declaration and Enqueue
1 #include <stdio.h>
2 #define MAX 5
3
4 int queue[MAX];
5 int front = -1, rear = -1;
6
7 void enqueue(int x) {
8 if (rear == MAX - 1) {
9 printf("Queue Overflow\n");
0 } else {
1 if (front == -1)
2 front = 0;
3 queue[++rear] = x;
4 }
5 }
6
C Programming March 24, 2026 12 / 14
Dequeue and Peek in Queue
1 int dequeue() {
2 if (front == -1 || front > rear) {
3 printf("Queue Underflow\n");
4 return -1;
5 }
6 return queue[front++];
7 }
8
9 int peek() {
0 if (front == -1 || front > rear) {
1 printf("Queue is Empty\n");
2 return -1;
3 }
4 return queue[front];
5 }
6
C Programming March 24, 2026 13 / 14
Any questions please?
Thank you!
C Programming March 24, 2026 14 / 14