0% found this document useful (0 votes)
10 views

Data Structures (Chapter-05 Queue)

The document presents an overview of queue data structures, emphasizing the First-In-First-Out (FIFO) principle and detailing various types of queues including simple, circular, double-ended, and priority queues. It outlines basic operations such as enqueue, dequeue, traverse, isFull, and isEmpty, along with their respective algorithms and implementations using arrays and linked lists. Additionally, it discusses the limitations of linear queues and introduces circular queues as a solution for efficient memory utilization.

Uploaded by

rout93596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Structures (Chapter-05 Queue)

The document presents an overview of queue data structures, emphasizing the First-In-First-Out (FIFO) principle and detailing various types of queues including simple, circular, double-ended, and priority queues. It outlines basic operations such as enqueue, dequeue, traverse, isFull, and isEmpty, along with their respective algorithms and implementations using arrays and linked lists. Additionally, it discusses the limitations of linear queues and introduces circular queues as a solution for efficient memory utilization.

Uploaded by

rout93596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA

STRUCTURES
WITH COMPETITIVE
CODING
Presented By: Mr. Satyananda Swain
Assistant Professor, Computer Science Engineering,

SCHOOL OF ENGINEERING AND TECHNOLOGY,


CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DATA STRUCTURES
Chapter-5: Queue
Presented By: Mr. Satyananda Swain
(Assistant Professor)
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY,
BHUBANESWAR, ODISHA
Introduction: Queue 1
❑ A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It operates like a line
where elements are added at one end (rear) and removed from the other end (front).

❑ FIFO(First In First Out) Principle in Queue Data Structure.

❑ A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e.
First Come First Serve). Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue(sometimes, head of the queue). Similarly, the position
of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Types of Queue Data Structure: 2

❑ Queue data structure can be classified into 4 types:

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Types of Queue Data Structure 3
❑ There are different types of queues:

❑ Simple Queue: Simple Queue simply follows a FIFO Structure. We can only insert the element at the back and remove the element from the front of the queue.

❑ Circular Queue: This is a special type of queue where the last position is connected back to the first position. Here also the operations are performed in FIFO
order.

❑ Double-Ended Queue (Dequeue): In a double-ended queue the insertion and deletion operations, both can be performed from both ends. They are of two
types:

▪ Input Restricted Queue: This is a simple queue. In this type of queue, the input can be taken from only one end but deletion can be done from any end.

▪ Output Restricted Queue: This is also a simple queue. In this type of queue, the input can be taken from both ends but deletion can be done from only
one end.

❑ Priority Queue: A priority queue is a special queue where the elements are accessed based on the priority assigned to them. They are of two types:

▪ Ascending Priority Queue: In Ascending Priority Queue, the elements are arranged in increasing order of their priority values. Element with smallest
priority value is popped first.

▪ Descending Priority Queue: In Descending Priority Queue, the elements are arranged in decreasing order of their priority values. Element with largest
priority is popper first.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Basic Operations on Linear Queue Data Structure 4

❑ Certain operations are provided to us to make manipulations in a Queue. They are:

▪ Enqueue: Adds (or stores) an element to the rear end of the queue.

▪ Dequeue: Removal of elements from the front end of the queue.

▪ Traverse: Display all the data elements available in the queue without

removing them.

▪ isFull: Validates if the queue is full.

▪ isEmpty: Validates if the queue is empty.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Implementation of Linear Queue Data Structure 5

❑ There are two ways to implement a Queue:

▪ Using Array

▪ Using Linked List

❑ To implement a queue using a simple array, create an array of size n and take two variables front and
rear, both of which will be initialized to 0, which means the queue is currently empty. Element:

▪ rear is the index up to which the elements are stored in the array and

▪ front is the index of the first element of the array.

❑ In a linked list-based implementation, the enqueue operation is implemented by creating a new node
with the new element and setting the next pointer of the current rear node to the new node. The dequeue
operation is implemented by setting the start pointer to point to the next pointer of the front node and
returning the value of the current front node.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Enqueue Operation 6
❑ Enqueue operation adds an item to the Queue. If the Queue is full, it is
considered an Overflow condition. Step 1: Start.

❑ Algorithm : ENQUEUE(QUEUE, FRONT, REAR, N, ITEM) Step 2: If (REAR = N), then:

▪ Here, the QUEUE is a Linear Array of elements following the FIFO Step 2.1) Print “OVERFLOW” and Return.
Principle. [End of if of Step-2]
▪ FRONT is a variable storing the index value of the Front element Step 3: If (FRONT = 0 and REAR = 0), then:
of the QUEUE.
Step 3.1) Set FRONT : = 1 and REAR : = 1.
▪ REAR is a variable storing the index value of the Rear or Last
Step 4: Else, then:
element of the QUEUE.
Step 4.1) Set REAR : = REAR + 1.
▪ N is the Maximum number of elements that can be stored in the
QUEUE. [End of if of Step-3]

▪ ITEM is the element to be inserted. Step 5: Set QUEUE[REAR] : = ITEM.

This algorithm is used to insert ITEM at the REAR end of QUEUE. Step 6: Stop.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Dequeue Operation 7
❑ Dequeue operation remove an item from the front end of the
Queue. If the Queue is empty, it is considered an Underflow Step 1: Start.

condition. Step 2: Let ITEM.

Step 3: If (FRONT = 0), then:


❑ Algorithm : DEQUEUE(QUEUE, FRONT, REAR)
Step 3.1) Print “UNDERFLOW” and Return.[End of if of Step-3]
▪ Here, the QUEUE is a Linear Array of elements following
Step 4: Set ITEM : = QUEUE[FRONT].
the FIFO Principle.
Step 5: If (FRONT = REAR), then:
▪ FRONT is a variable storing the index value of the Front
Step 5.1) Set FRONT : = 0 and REAR : = 0.
element of the QUEUE.
Step 6: Else, then:
▪ REAR is a variable storing the index value of the Rear or Step 6.1) Set FRONT : = FRONT + 1. [End of if of Step-5]
Last element of the QUEUE.
Step 7: Print “Item Deleted=”, ITEM.
This algorithm is used to remove an element from FRONT end of Step 8: Stop.
QUEUE and assign it to ITEM.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Traverse Operation 8
❑ Traverse operation display the elements of the Queue in

FIFO order from FRONT to REAR.


Step 1: Start.

Step 2: Let I.
❑ Algorithm: TRAVERSE_QUEUE (QUEUE, FRONT, REAR)
Step 3: If (FRONT = 0), then:
▪ Here, the QUEUE is a Linear Array of elements following
Step 3.1) Print “Empty Queue” and Return.
the FIFO Principle.
[End of if of Step-3]
▪ FRONT is a variable storing the index value of the Front
Step 4: Repeat For I : = FRONT to REAR incremented
element of the QUEUE.
by 1, then:
▪ REAR is a variable storing the index value of the Rear
Step 4.1) Apply PROCESS/ Print QUEUE[I].
or Last element of the QUEUE.
[End of Loop of Step-4]
This algorithm is used to display all the elements of QUEUE in
Step 5: Stop.
FIFO order.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
isEmpty Operation 9

❑ isEmpty() operation returns true if the Queue is empty; otherwise, it is false.

❑ Algorithm for isEmpty( ) Operation:

▪ Check for the value of the Front and Rear of Queue.

▪ Algorithmically, If (FRONT = 0), then the Queue is Empty so return true.

▪ Otherwise, the Queue is not empty, so return false.

▪ Programmatically, If (FRONT == -1), then the Queue is empty so return true.

▪ Otherwise, the Queue is not Empty, so return false.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
isFull Operation 10

❑ isFull() operation returns true if the Queue is full; otherwise, it is false.

❑ Algorithm for isFull( ) Operation:

▪ Check for the value of the Front and Rear of Queue.

▪ Algorithmically, If (REAR = N), then the Queue is full so return true.

▪ Otherwise, the Queue is not full, so return false.

▪ Programmatically, If (REAR == N-1), then the Queue is full so return true.

▪ Otherwise, the Queue is not full, so return false.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Linear Queue Operations: Complete C Code 11
❑ C-CODE return 1;

//C program to simulate the Linear Queue operations. else

#include<stdio.h> return 0;

#include<stdlib.h> }

#define MAX 5 int isfull(queue *q)

typedef struct {

{ if(q->r==MAX-1)

int f,r,a[MAX]; return 1;

}queue; else

int isempty(queue *q) return 0;

{ }

if(q->f==-1 )
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Linear Queue Operations: Complete C Code 12
void display(queue *q) {

{ if(isfull(q))

int i; printf("\nOVERFLOW\n");

if(isempty(q)) else if(q->f==-1 && q->r==-1)

printf("\nQueue Empty\n"); {

else q->f=0;q->r=0;q->a[q->r]=x;

{ }

printf("\nElements in the queue are:\n"); else

for(i=q->f;i<=q->r;i++) {

printf("%d\t",q->a[i]); q->r++;

printf("\n\n"); q->a[q->r]=x;

} }

} display(q);

void qinsert(queue *q, int x) }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Linear Queue Operations: Complete C Code 13
void qdelete(queue *q) else

{ q->f++;

int x; printf("Deleted Item=%d",x);

if(isempty(q)) display(q);

{ }

printf("\nUNDERFLOW\n"); return;

x=q->a[q->f];

if(q->f==q->r)

q->f=q->r=-1;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Linear Queue Operations: Complete C Code 14
void main() case 1: printf("\nEnter Item:\n");

{ scanf("%d",&item);

queue q; qinsert(&q,item);break;

int item,option; case 2: qdelete(&q);break;

q.f=-1;q.r=-1; case 3: display(&q);break;

do case 4: exit(0);

{ default : printf("\nWrong Choice\n");

printf("\nPRESS:\n1->INSERT\n2->DELETE\n3->DISPLAY\n }

4->EXIT\nEnter your chice:\n"); }while(option<5);

scanf("%d",&option); }

switch(option)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Limitation of Linear Queue 15

❑ If the last position of the queue is occupied, it is not possible to enqueue any more
elements even though some positions are vacant towards the front positions of the
queue.

❑ On deleting an element from the existing queue, the front pointer is shifted to the next
position. •This results in the virtual deletion of an element. By doing so, memory space
occupied by deleted elements is wasted, hence inefficient memory utilization.

❑ To overcome the disadvantage of the linear queue, the circular queue is used.

❑ We can solve this problem by joining the front and rear ends of a queue to make the
queue a circular queue. In a circular queue, the last node is connected to the first node
to make a circle.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue 16

❑ A Circular Queue is an extended version of a linear queue where the last element of the queue is
connected to the first element of the queue forming a circle.

❑ The operations are performed based on FIFO (First In First Out) principle. It is also called ‘Ring
Buffer’.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Basic Operations on Circular Queue Data Structure 17

❑ Certain operations are provided to us to make manipulations in a CQueue. They are:

▪ Enqueue: Adds (or stores) an element to the rear end of the queue.

▪ Dequeue: Removal of elements from the front end of the queue.

▪ Traverse: Display all the data elements available in the queue without removing them.

▪ isFull: Validates if the queue is full.

▪ isEmpty: Validates if the queue is empty.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue Operations 18

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Implementation of Circular Queue Data Structure 19

❑ There are two ways to implement a Queue:

▪ Using Array

▪ Using Linked List

❑ To implement a circular queue using a simple array, create an array of size n and take two variables
front and rear, both of which will be initialized to 0, which means the queue is currently empty. Element:

▪ rear is the index up to which the elements are stored in the array and

▪ front is the index of the first element of the array.

❑ In a linked list-based implementation, the enqueue operation is implemented by creating a new node
with the new element and setting the next pointer of the current rear node to the new node. The dequeue
operation is implemented by setting the start pointer to point to the next pointer of the front node and
returning the value of the current front node.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Enqueue Operation 20
❑ Enqueue operation adds an item to the circular queue. If the
circular queue is full, it is considered an Overflow condition. Step 1: Start.

❑ Algorithm : CQINSERT(CQUEUE, FRONT, REAR, N, ITEM) Step 2: If (FRONT = 1 and REAR = N or FRONT = REAR + 1), then:

Step 2.1) Print “OVERFLOW” and Return. [End of if of Step-2]


▪ Here, the CQUEUE is a Linear Array of elements following the
FIFO Principle with a circular nature. Step 3: If (FRONT = 0 and REAR = 0), then:

▪ FRONT is a variable storing the index value of the Front Step 3.1) Set FRONT : = 1 and REAR : = 1.
element of the CQUEUE. Step 4: Else if (REAR = N), then:

▪ REAR is a variable storing the index value of the Rear or Last Step 4.1) Set REAR : = 1.
element of the CQUEUE.
Step 5: Else, then:
▪ N is the Maximum number of elements stored in the QUEUE. Step 5.1) Set REAR : = REAR + 1. [End of if of Step-3]
▪ ITEM is the element to be inserted. Step 5: Set CQUEUE[REAR] : = ITEM.
This algorithm is used to insert ITEM at the REAR end of CQUEUE. Step 6: Stop.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Dequeue Operation 21

❑ Dequeue operation removes an item from the front end of Step 1: Start.
the circular queue. If the circular queue is empty, it is Step 2: Let ITEM.
considered an Underflow condition. Step 3: If (FRONT = 0), then:

❑ Algorithm : CQDELETE(CQUEUE, FRONT, REAR) Step 3.1) Print “UNDERFLOW” and Return.[End of if of Step-3]

Step 4: Set ITEM : = CQUEUE[FRONT].


▪ Here, the CQUEUE is a Linear Array of elements
following the FIFO Principle with a circular nature. Step 5: If (FRONT = REAR), then:

Step 5.1) Set FRONT : = 0 and REAR : = 0.


▪ FRONT is a variable storing the index value of the
Step 4: Else if (FRONT = N), then:
Front element of the CQUEUE.
Step 4.1) Set FRONT : = 1.
▪ REAR is a variable storing the index value of the Rear
Step 6: Else, then:
or Last element of the CQUEUE.
Step 6.1) Set FRONT : = FRONT + 1. [End of if of Step-5]
This algorithm removes an element from the FRONT end of
Step 7: Print “Item Deleted=”, ITEM.
QUEUE and assigns it to ITEM.
Step 8: Stop.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Traverse Operation 22

❑ Traverse operation displays the circular queue Step 1: Start.

elements in FIFO order from FRONT to REAR. Step 2: Let I.

Step 3: If (FRONT = 0 and REAR = 0), then:


❑ Algorithm: TRAVERSE_CQUEUE (CQUEUE, FRONT, REAR)
Step 3.1) Print “Empty Queue” and Return.[End of if of Step-3]

▪ Here, the CQUEUE is a Linear Array of elements Step 4: If(FRONT < = REAR), then:

Step 4.1)Repeat For I : = FRONT to REAR incremented by 1, then:


following the FIFO Principle with a circular nature.
Step 4.1.1) Apply PROCESS/ Print CQUEUE[I]. [End of Loop of Step-4.1]
▪ FRONT is a variable storing the index value of the Step 5: Else, then:

Front element of the CQUEUE. Step 5.1)Repeat For I : = FRONT to N incremented by 1, then:

Step 5.1.1) Apply PROCESS/ Print CQUEUE[I]. [End of Loop of Step-5.1]


▪ REAR is a variable storing the index value of the
Step 5.2)Repeat For I : = 1 to REAR incremented by 1, then:
Rear or Last element of the CQUEUE. Step 5.2.1) Apply PROCESS/ Print CQUEUE[I]. [End of Loop of Step-5.2]

[End of if of step-4]
This algorithm displays all the elements of CQUEUE in FIFO
Step 6: Stop.
order.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
isEmpty Operation 23

❑ isEmpty() operation returns true if the CQueue is empty; otherwise, it is false.

❑ Algorithm for isEmpty( ) Operation:

▪ Check for the value of the Front and Rear of CQueue.

▪ Algorithmically, If (FRONT = 0), then the CQueue is Empty, so return true.

▪ Otherwise, the CQueue is not empty, so return false.

▪ Programmatically, If (FRONT == -1), then the CQueue is empty, so return true.

▪ Otherwise, the CQueue is not Empty, so return false.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
isFull Operation 24

❑ isFull() operation returns true if the CQueue is full; otherwise, it is false.

❑ Algorithm for isFull( ) Operation:

▪ Check for the value of the Front and Rear of CQueue.

▪ Algorithmically, If (FRONT = 1 and REAR = N or FRONT = REAR + 1), then the CQueue is full

so return true.

▪ Otherwise, the CQueue is not full, so return false.

▪ Programmatically, If (FRONT == 0 and REAR == N-1 or FRONT = REAR + 1), then the CQueue

is full so return true.

▪ Otherwise, the CQueue is not full, so return false.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue Operations: Complete C Code 25
❑ C-CODE return 1;

//C program to simulate the CQueue operations. else

#include<stdio.h> return 0;

#include<stdlib.h> }

#define MAX 5 int isfull(cqueue *q)

typedef struct {

{ if(q->f==0 && q->r==MAX-1 || q->f==q->r+1)

int f,r,a[MAX]; return 1;

}cqueue; else

int isempty(cqueue *q) return 0;

{ }

if(q->f==-1) DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue Operations: Complete C Code 26
void display(cqueue *q) printf("%d\t",q->a[i]);

{ else

int i; {

if(isempty(q)) for(i=q->f;i<=MAX-1;i++)

{ printf("%d\t",q->a[i]);

printf("\nCrcular Queue Empty\n"); for(i=0;i<=q->r;i++)

} printf("%d\t",q->a[i]);

else }

{ printf("\n\n");

printf("\nElements in the CQueue are:\n"); }

if(q->f<=q->r) }

for(i=q->f;i<=q->r;i++)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circuar Queue Operations: Complete C Code 27
void cqinsert(cqueue *q, int x) {

{ q->r=0;

if(isfull(q)) q->a[q->r]=x;

{ }

printf("\nOVERFLOW\n"); else

} {

else if(q->f==-1 && q->r==-1) q->r++;

{ q->a[q->r]=x;

q->f=0;q->r=0;q->a[q->r]=x; }

} display(q);

else if(q->r==MAX-1) }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue Operations: Complete C Code 28

void cqdelete(cqueue *q) q->f=q->r=-1;

{ else if (q->f==MAX-1)

int x; q->f=0;

if(isempty(q)) else

{ q->f++;

printf("\nUNDERFLOW\n"); printf("Deleted Item=%d",x);

return; display(q);

} }

x=q->a[q->f];

if(q->f==q->r)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Circular Queue Operations: Complete C Code 29
void main() {

{ case 1: printf("\nEnter Item:\n");

cqueue cq; scanf("%d",&item);

int item,option; cqinsert(&cq,item);break;

cq.f=-1;cq.r=-1; case 2: cqdelete(&cq);break;

do case 3: display(&cq);break;

{ case 4: exit(0);

printf("\nPRESS:\n1->INSERT\n2->DELETE\n3- default:printf("\nWrong Choice\n");


>DISPLAY\n4->EXIT\nEnter your chice:\n");
} }while(option<5);
scanf("%d",&option);
}
switch(option)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Double Ended Queue(Deque) 30

❑ Double-ended Queue is also a Queue data structure in which the insertion and deletion operations are

performed at both ends (front and rear). That means, we can insert at both front and rear positions and

delete from both front and rear positions.

❑ Types of deque: There are two types of deque:

▪ Input restricted queue

▪ Output restricted queue


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Operations on Double Ended Queue(Deque) 31

❑ Operations on Deque are :


Operation Description
Insert_front() Inserts the element at the beginning.
Insert_Rear() Inserts the element at the end.
Delete_front() Removes the first element from the deque.

Delete_Rear() Removes the last element from the deque.

front() Gets the front element from the deque.

Rear() Gets the last element from the deque.

empty() Checks whether the deque is empty or not.

full() Checks whether the deque is full or not.

size() Determines the number of elements in the deque.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Input-Restricted Double-Ended Queue 32

❑ In this type of Queue, the input can be taken from one side only(rear) and deletion of elements can be

done from both sides(front and rear). This kind of Queue does not follow FIFO(first in, first out). This

queue is used in cases where the data consumption needs to be in FIFO order but if there is a need to

remove the recently inserted data for some reason.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Output-Restricted Double-Ended Queue 33

❑ In this type of Queue, the input can be taken from both sides(rear and front) and the deletion of the

element can be done from only one side(front). This queue is used in the case where the inputs have

some priority order to be executed and the input can be placed even in the first place so that it is

executed first.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Priority Queue 34
❑ A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher

priority values are typically retrieved before elements with lower priority values. There are two types of Priority

Queues. They are:

❑ Ascending Priority Queue: Element can be inserted arbitrarily, but only the smallest element can be removed. For

example, suppose an array has elements 4, 2, and 8 in the same order. So, while inserting the elements, the

insertion will be in the same sequence but the order will be 2, 4, 8 while deleting.

❑ Descending priority Queue: Element can be inserted arbitrarily but only the largest element can be removed first

from the given Queue. For example, suppose an array has elements 4, 2, and 8 in the same order. So, while inserting

the elements, the insertion will be in the same sequence but while deleting, the order will be 8, 4, 2.

❑ There are several ways to implement a priority queue, including using an array, linked list, heap, or BST.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Advantages of Queue 35
Queues provide several key advantages that make them a ubiquitous data structure in programming. The main benefits

are:

❑ Ordering: Queues maintain the original order of insertion for elements. The first element inserted is also the first

one out. This First-In-First-Out (FIFO) ordering ensures sequenced access and processing..

❑ Synchronization: Queues can synchronize and transfer data safely between multiple threads or processes.

Elements enqueued are processed in an ordered way by consumer threads.

❑ Decoupling: Queues allow the decoupling of different components or services in a system. The queue acts as an

intermediary buffer for reliable delivery.

❑ Load Leveling: Queues smooth uneven loads and prevent resource overloading. Temporary spikes in traffic or load

are absorbed efficiently by the queue.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Advantages of Queue 36
❑ Asynchrony: Queues facilitate asynchronous, non-blocking operations. Elements needing processing or I/O

operations are enqueued. The queue allows resuming when the result is available.

❑ Fault Tolerance: Queues implement reliable delivery even across system failures. Elements persist in the queue

until fully processed. Failed elements are required.

❑ Traffic Burst Buffering: Queues can buffer short bursts of traffic. Temporary incoming spikes beyond service

capacity are absorbed in the queue till they can be handled.

❑ Resource Pooling: Queues allow pooling of servicing resources like threads and database connections. Resources

pick up queue elements for processing in turn.

❑ Rate Limiting: Queues can be used to rate limit tasks and prevent resource overuse. Elements are added to a queue

and processed at a defined rate. Additional elements remain queued.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Disadvantages of Queue 37

Although queues provide many benefits, they also come with some disadvantages:

❑ No Random Access: Queues only allow sequential access to elements. There is no way to access an arbitrary

element in the queue directly. Elements must be dequeued in order from the front. This makes queues unsuitable

for applications requiring random access to data.

❑ Additional Memory Overhead: Queues require extra memory to handle the buffering and ordering of elements. All

elements must be stored until processed. For a system processing data "in place", a queue solution incurs

overhead.

❑ Increased Processing Latency: Queues inherently introduce delays as elements wait to be processed in order.

Elements queued earlier must be completed before later ones begin processing. This increases processing latency.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Disadvantages of Queue 38
❑ No Preemption: Queues do not allow preempting already queued elements. Once queued, elements cannot be

removed until they reach the font. Priority changes cannot be accommodated.

❑ Additional Programming Complexity: Queues require managing the order, storage and access of elements.

Additional code must handle queue insertion, removal and overflow. This increases software complexity.

❑ Not Cache Friendly: Queues lack locality of reference and access elements sequentially. This prevents efficient use

of caches during access. Caches work best with localized random access.

❑ Restricted Access: Queues only allow access to the front and rear elements. The only operations available are

enqueue and dequeue. This restricts the ability to organize and access data.

❑ Blocking Operations: Operations like dequeue can block and stall if the queue is empty. This can lead to poor

performance and starvation for other waiting processes. Care must be taken to prevent blocking where possible.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Applications of Queue 39

Queues have various real-world applications since they allow efficient modelling of real queues and Ordering

tasks. Some of the major applications are:

❑ Operating Systems Scheduling: One of the most common uses of queues is for scheduling tasks in

operating systems. The OS maintains queues of processes waiting for important resources like CPU time,

memory, I/O devices, etc. Schedulers follow queue disciplines like FCFS, Priority Queueing, and round-

robin to decide the next resource allocation process.

❑ Message Queuing: Message queues are used frequently in distributed applications. Various components

of an application interact by passing asynchronous messages. Queues store these messages reliably until

they are consumed. Message queues enable smooth data exchange between distributed systems.
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Applications of Queue 40

❑ Breadth-First Search: Queues are leveraged in graph and tree algorithms like breadth-first search to

traverse nodes layer by layer. Initially, the root node is enqueued. All its neighbours are then dequeued

and enqueued. This continues until the entire graph is traversed.

❑ Printer Spooler: Operating systems and printer drivers use print queues to handle print requests. All

documents are spooled in a queue instead of being printed directly. The printer handles jobs sequentially

in a FIFO manner. Queues allow smooth handling of frequent print requests and prevent bottlenecks.

❑ Web Server Request Queuing: Web servers and frameworks leverage request queues to handle multiple

client requests concurrently. As requests come in, they are added to a queue and processed in order. The

HTTP requests may be queued based on priority.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
Applications of Queue 41

❑ CPU Scheduling: OS kernels use queues for CPU scheduling. The processes waiting for CPU time are kept

in a queue. This queue-based scheduling improves CPU efficiency.

❑ Traffic Modeling: Traffic systems maintain queues of vehicles waiting at signals, toll booths, etc. These

queues are modeled using queuing theory to analyze traffic patterns and optimize road networks.

❑ Order Processing: E-commerce systems leverage queues to smooth order processing and ensure

sequencing.

❑ Load Balancing: Queues can be used for load balancing in distributed systems. A central job queue is

created, and multiple worker processes are used to listen for jobs. Load balancing is a key application of

queues in building distributed systems.


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF ENGINEERING AND TECHNOLOGY, CENTURION UNIVERSITY, BHUBANESWAR, ODISHA

You might also like