In this article Queue in data structure we give the information about Queue is a waiting list or other means of organizing people or object into a First-In-First-Out (FIFO) order.
Queue in Data Structure
Introduction to Queue
A queue is a linear data structure that follows the FIFO (First-In-First-Out) principle —
the element inserted first is the one removed first.
Real-life example:
A line of people waiting at a ticket counter — the person who comes first is served first.
In computing, a queue is used to manage tasks or requests — like print jobs or CPU processes.
Definition of Queue
“Queue is an ordered list of elements in which insertion takes place at one end (rear) and deletion takes place at the other end (front).”
- It is a non-primitive linear data structure.
- Operations follow FIFO order.
Basic Queue Operations
| Operation | Description |
| Insert() / Enqueue() | Adds an element to the rear end of the queue. |
| Delete() / Dequeue() | Removes an element from the front end of the queue. |
| IsFull() | Checks whether the queue is full. |
| IsEmpty() | Checks whether the queue is empty. |
| Display() | Displays all elements in the queue. |
Working of Queue
Insertion (Enqueue Operation)
Elements are added from the rear end.
Queue: [ ] → Empty
Insert 15 → [15]
Insert 25 → [15, 25]
Insert 35 → [15, 25, 35]
Insert 45 → [15, 25, 35, 45]
Insert 55 → [15, 25, 35, 45, 55]
Deletion (Dequeue Operation)
Elements are removed from the front end.
Queue: [15, 25, 35, 45, 55]
Delete 15 → [25, 35, 45, 55]
Delete 25 → [35, 45, 55]
Delete 35 → [45, 55]
Delete 45 → [55]
Delete 55 → Queue Empty
Declaration of Queue
Queues can be implemented:
- Statically using arrays
- Dynamically using linked lists
For static implementation using array:
#define MAX 5
struct queue {
int data[MAX];
int front, rear;
};
struct queue q;
Here:
- front → points to the first element
- rear → points to the last element
Queue Operations using Array
- Check if Queue is Full
int isFull() {
if (q.rear == MAX – 1)
return 1;
else
return 0;
}
- Check if Queue is Empty
int isEmpty() {
if (q.front == -1 || q.front > q.rear)
return 1;
else
return 0;
}
- Insert Operation
void insert(int no) {
if (q.front == -1)
q.front = 0;
q.rear = q.rear + 1;
q.data[q.rear] = no;
printf(“\n%d is inserted into Queue.”, no);
}
- Delete Operation
void delete() {
int temp;
temp = q.data[q.front];
q.front = q.front + 1;
printf(“\n%d element is deleted.”, temp);
}
- Display Operation
void display() {
int i;
printf(“\nQueue elements are: “);
for (i = q.front; i <= q.rear; i++)
printf(“%d “, q.data[i]);
}
Complete Program: Queue Implementation using Array
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
struct queue {
int front, rear;
int data[MAX];
};
struct queue q;
int isFull();
int isEmpty();
void insert(int);
void delete();
void display();
void main() {
int n, ch;
q.front = -1;
q.rear = -1;
while (1) {
printf(“\n\n—- MENU —-“);
printf(“\n1. Insert\n2. Delete\n3. Display\n4. Exit”);
printf(“\nEnter your choice: “);
scanf(“%d”, &ch);
switch (ch) {
case 1:
if (isFull())
printf(“\nQueue Overflow!”);
else {
printf(“\nEnter number to insert: “);
scanf(“%d”, &n);
insert(n);
}
break;
case 2:
if (isEmpty())
printf(“\nQueue Underflow!”);
else
delete();
break;
case 3:
if (isEmpty())
printf(“\nQueue is Empty!”);
else
display();
break;
case 4:
exit(0);
default:
printf(“\nInvalid choice! Enter 1 to 4 only.”);
}
}
}
int isFull() {
return (q.rear == MAX – 1);
}
int isEmpty() {
return (q.front == -1 || q.front > q.rear);
}
void insert(int no) {
if (q.front == -1)
q.front = 0;
q.rear++;
q.data[q.rear] = no;
printf(“\n%d inserted into Queue.”, no);
}
void delete() {
int temp = q.data[q.front];
q.front++;
printf(“\n%d element deleted.”, temp);
}
void display() {
int i;
printf(“\nQueue Elements: “);
for (i = q.front; i <= q.rear; i++)
printf(“%d “, q.data[i]);
}
Output (Example Run)
—- MENU —-
- Insert
- Delete
- Display
- Exit
Enter your choice: 1
Enter number to insert: 10
10 inserted into Queue.
Enter your choice: 1
Enter number to insert: 20
20 inserted into Queue.
Enter your choice: 3
Queue Elements: 10 20
Enter your choice: 2
10 element deleted.
Enter your choice: 3
Queue Elements: 20
Applications of Queue
- CPU Scheduling & Disk Scheduling —
Used to manage multiple processes waiting for CPU or disk access. - Data Transfer & Synchronization —
Used in I/O buffers, pipes, and communication channels. - Print Spooling —
Documents are queued before being printed. - Graph Traversal (BFS) —
Breadth-First Search algorithm uses queue. - Interrupt Handling —
Interrupts are handled in the order they occur. - Call Center Systems —
Calls are placed in a queue until an executive becomes available.
Key Points
- Follows FIFO principle.
- Implemented using Array or Linked List.
- Supports Insert (Enqueue) and Delete (Dequeue) operations.
- Used in scheduling, buffering, and data management.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining