UNIT 3: QUEUE
Queue Data Structure
A queue can be defined as an ordered list which enables insert operations to be performed
at one end called REAR and delete operations to be performed at another end called FRONT.
Queue is referred to be as First In First Out list.
Applications of Queue
waiting lists for a single shared resource like printer, disk, CPU.
asynchronous transfer of data
maintain the play list in media players in order
operating systems for handling interrupts
Basic Operations on Queue:
enqueue() – Insertion of elements to the queue.
dequeue() – Removal of elements from the queue.
peek() or front()- Acquires the data element available at the front node of the queue
without deleting it.
rear() – This operation returns the element at the rear end without removing it.
isFull() – Validates if the queue is full.
isEmpty() – Checks if the queue is empty.
size(): This operation returns the size of the queue i.e. the total number of elements it
contains.
Array implementation of queue
To implement a queue using a simple array,
create an array arr of size n and
take two variables front and rear which are initialized as 0 and -1 respectively
rear is the index up to which the elements are stored in the array including the rear
index itself. We mainly add an item by incrementing it.
front is the index of the first element of the array. We mainly remove the element at
this index in Dequeue operation.
Enqueue
If rear == capacity-1, Overflow condition
Dequeue
If rear == -1, Underflow condition
Here's a simple implementation of a queue using an array in C:
#include <stdio.h>
#define MAX_SIZE 5
// Queue structure
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
int size;
} Queue;
// Function to create a new queue
void createQueue(Queue *q) {
q->front = q->rear = q->size = 0;
}
// Function to check if queue is empty
int isEmpty(Queue *q) {
return q->size = 0;
// Function to check if queue is full
int isFull(Queue *q) {
return q->size = MAX_SIZE;
// Function to enqueue an element
void enqueue(Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full\n");
return;
q->data[q->rear] = value;
q->rear = (q->rear + 1) % MAX_SIZE;
q->size++;
// Function to dequeue an element
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
int value = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return value;
// Function to peek at the front element
int peek(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
return q->data[q->front];
// Function to print queue elements
void printQueue(Queue *q) {
int i = q->front;
for (int count = 0; count < q->size; count++) {
printf("%d ", q->data[i]);
i = (i + 1) % MAX_SIZE;
printf("\n");
int main() {
Queue q;
createQueue(&q);
int choice, value;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Is Full\n");
printf("5. Is Empty\n");
printf("6. Print Queue\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
printf("Dequeued value: %d\n", dequeue(&q));
break;
case 3:
printf("Front element: %d\n", peek(&q));
break;
case 4:
printf("Is Full: %s\n", isFull(&q) ? "Yes" : "No");
break;
case 5:
printf("Is Empty: %s\n", isEmpty(&q) ? "Yes" : "No");
break;
case 6:
printf("Queue elements: ");
printQueue(&q);
break;
case 7:
return 0;
default:
printf("Invalid choice\n");
return 0;
Here's the pseudo code for the Queue implementation:
Create Queue
1. Initialize Queue q
2. Set q.front = 0
3. Set q.rear = 0
4. Set q.size = 0
Is Empty
1. If q.size == 0
2. Return True
3. Else
4. Return False
Is Full
1. If q.size == MAX_SIZE
2. Return True
3. Else
4. Return False
Enqueue
1. If Is Full(q)
2. Print "Queue is full"
3. Else
4. q.data[q.rear] = value
5. q.rear = (q.rear + 1) mod MAX_SIZE
6. q.size++
Dequeue
1. If Is Empty(q)
2. Print "Queue is empty"
3. Else
4. value = q.data[q.front]
5. q.front = (q.front + 1) mod MAX_SIZE
6. q.size--
7. Return value
Print Queue
1. For i = q.front to q.size
2. Print q.data[i]
3. i = (i + 1) mod MAX_SIZE
Main
1. Create Queue q
2. Enqueue(q, 1)
3. Enqueue(q, 2)
4. Enqueue(q, 3)
5. Enqueue(q, 4)
6. Enqueue(q, 5)
7. Print Queue(q)
8. Dequeue(q)
9. Print Queue(q)
Queue – Linked List Implementation
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
// Queue structure
typedef struct {
Node* front;
Node* rear;
} Queue;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
// Function to create a new queue
void createQueue(Queue* q) {
q->front = q->rear = NULL;
// Function to check if queue is empty
int isEmpty(Queue* q) {
return q->front == NULL;
// Function to enqueue an element
void enqueue(Queue* q, int data) {
Node* newNode = createNode(data);
if (isEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
// Function to dequeue an element
int dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
int data = q->front->data;
Node* temp = q->front;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
free(temp);
return data;
// Function to peek at the front element
int peek(Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
return q->front->data;
}
// Function to print queue elements
void printQueue(Queue* q) {
Node* temp = q->front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
int main() {
Queue q;
createQueue(&q);
int choice, data;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Print Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &data);
enqueue(&q, data);
break;
case 2:
printf("Dequeued value: %d\n", dequeue(&q));
break;
case 3:
printf("Front element: %d\n", peek(&q));
break;
case 4:
printf("Queue elements: ");
printQueue(&q);
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
return 0;
Pseudo Code
Create Node
1. Create node newNode
2. Set newNode.data to data
3. Set newNode.next to NULL
4. Return newNode
Create Queue
1. Initialize queue q
2. Set q.front to NULL
3. Set q.rear to NULL
Is Empty
1. If q.front is NULL
2. Return True
3. Else
4. Return False
Enqueue
1. Create node newNode with data
2. If q is empty
3. Set q.front to newNode
4. Set q.rear to newNode
5. Else
6. Set q.rear.next to newNode
7. Set q.rear to newNode
Dequeue
1. If q is empty
2. Print "Queue is empty"
3. Return -1
4. Set data to q.front.data
5. Set temp to q.front
6. Set q.front to q.front.next
7. If q.front is NULL
8. Set q.rear to NULL
9. Free temp
10. Return data
Peek
1. If q is empty
2. Print "Queue is empty"
3. Return -1
4. Return q.front.data
Print Queue
1. Set temp to q.front
2. While temp is not NULL
3. Print temp.data
4. Set temp to temp.next
Main
1. Create queue q
2. While true
3. Print menu
4. Get user choice
5. Switch choice
1. Enqueue: Get data, enqueue data
2. Dequeue: Dequeue, print dequeued value
3. Peek: Print front element
4. Print Queue: Print queue elements
5. Exit: Exit program
Priority Queue
A priority queue is a special type of queue in which each element is associated with
a priority value
Difference between Priority Queue and Normal Queue
In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the values
are removed on the basis of priority. The element with the highest priority is removed first.
Priority Queue Operations
1. Inserting an Element into the Priority Queue
Insert an element at the end of the queue
Heapify after insertion
Algorithm for insertion of an element into priority queue (max-heap)
If there is no node,
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)
heapify the array
2. Deleting an Element from the Priority Queue
Select the element to be deleted
Swap with the last leaf node element
Remove the last element leaf
Heapify the priority queue
Algorithm for deletion of an element in the priority queue (max-heap)
If nodeToBeDeleted is the leafNode
remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted
heapify the array
3. Peeking from the Priority Queue (Find max/min)
return rootNode
Priority Queue Applications
Dijkstra's algorithm
for implementing stack
for load balancing and interrupt handling in an operating system
Dynamic Memory Allocation Techniques:
1. malloc
int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
2. calloc
int* ptr = (int*) calloc(5, sizeof(int)); // Allocates memory for an array of 5 integers
3. realloc:
int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
ptr = (int*) realloc(ptr, 10 * sizeof(int)); // Reallocates memory for 10 integers