DS Lab · Experiment 6
Queue using an array
CStacks and queuesJNTUH R22 · CS306PC
Aim
To write a menu-driven C program that implements a linear queue using an array, with the operations enqueue (insert), dequeue (delete) and display, and reports queue overflow and underflow.
Algorithm
- Declare an array
queue[MAX]and two indexesfront = -1andrear = -1. −1 in both means the queue is empty. - Enqueue x: if
rear == MAX - 1, report overflow. Otherwise, if the queue was empty setfront = 0; then increaserearand store x inqueue[rear]. - Dequeue: if
front == -1, report underflow. Otherwise takequeue[front]and increasefront. - If
fronthas moved pastrear, the queue is empty again: resetfrontandrearto −1. - Display: print the elements from
queue[front]toqueue[rear]. - Repeat the menu until the user chooses Exit.
Program
#include <stdio.h>
#define MAX 3 /* kept small so that overflow shows quickly */
int queue[MAX];
int front = -1, rear = -1; /* -1 means the queue is empty */
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue overflow, cannot insert %d\n", value);
return;
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("Inserted %d\n", value);
}
void dequeue(void) {
if (front == -1) {
printf("Queue underflow, nothing to delete\n");
return;
}
printf("Deleted %d\n", queue[front]);
front++;
if (front > rear) { /* last element removed: queue is empty */
front = -1;
rear = -1;
}
}
void display(void) {
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue (front to rear): ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
int main(void) {
int choice, value;
printf("QUEUE USING ARRAY (size %d)\n", MAX);
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
do {
printf("\nEnter your choice: ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input\n");
break;
}
switch (choice) {
case 1:
printf("Enter value to insert: ");
if (scanf("%d", &value) == 1) {
enqueue(value);
}
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting.\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}Output
QUEUE USING ARRAY (size 3)
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Queue underflow, nothing to delete
Enter your choice: 1
Enter value to insert: 10
Inserted 10
Enter your choice: 1
Enter value to insert: 20
Inserted 20
Enter your choice: 1
Enter value to insert: 30
Inserted 30
Enter your choice: 1
Enter value to insert: 40
Queue overflow, cannot insert 40
Enter your choice: 3
Queue (front to rear): 10 20 30
Enter your choice: 2
Deleted 10
Enter your choice: 3
Queue (front to rear): 20 30
Enter your choice: 2
Deleted 20
Enter your choice: 2
Deleted 30
Enter your choice: 2
Queue underflow, nothing to delete
Enter your choice: 3
Queue is empty
Enter your choice: 4
Exiting.Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
MAX is set to 3 only so that overflow appears in a short run. Elements join at the rear and leave from the front, so they come out in the same order they went in.
This is a simple linear queue. Its weakness: after some dequeues, the slots before front are never used again, so the queue can report overflow while it still has free slots (unless it becomes completely empty and resets). A circular queue fixes this by wrapping rear back to 0 with rear = (rear + 1) % MAX.
Common mistakes
Forgetting to set
front = 0on the first insertion, sofrontstays at −1 and the first dequeue readsqueue[-1].Checking for an empty queue with
front == rear. That is true when the queue has exactly one element. Usefront == -1(or reset both to −1 when the last element leaves).Removing from the rear instead of the front, which turns the queue into a stack.
Checking overflow with
rear == MAXinstead ofrear == MAX - 1, which writes past the end of the array.
Viva questions
Try to answer each one aloud before you open it.
What is a queue?
A queue is a linear data structure where elements are inserted at one end (the rear) and removed from the other end (the front). The first element inserted is the first one removed, so it is FIFO (First In, First Out).
What are the drawbacks of a linear queue implemented with an array?
The size is fixed, and space freed at the front by dequeues cannot be reused, so the queue can show overflow even when slots are free.
How does a circular queue solve this problem?
It treats the array as a circle. When
rearreaches the last index it wraps to index 0 using(rear + 1) % MAX, so the freed slots at the front are reused.What is the time complexity of enqueue and dequeue?
Both are O(1), because each changes one index and one array element.
Give some applications of queues.
Printer job queues, CPU scheduling, handling requests at a server, and breadth-first search of a graph.