0% found this document useful (0 votes)
28 views5 pages

R23 - DS - Unit IV-1

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)
28 views5 pages

R23 - DS - Unit IV-1

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/ 5

Unit IV [R23]

Queues: Introduction to queues: properties and operations, implementing queues using arrays and
linked list, Applications of queues in breadth-first search, scheduling etc.
Circular Queues: Introduction to circular queues, Operations and their applications.

Introduction to Queues:
 Queue is also a linear data structure in which elements are inserted at one end called rear end
and elements are deleted from another end called front end.
 It works on the principle called First in First out mechanism. So sometimes the queue is called
as FIFO list.
 Hence the first element is inserted into the queue is the element to come out first from the
queue.
 There are many real life examples for queues. For example a queue of people waiting at bank.

Operations on Queues:
There are two operations that we can perform on queues. They are
1. Enqueue
2. Dequeue
1. Enqueue: This operation is inserting an element at Rear end of the queue. At the time of
insertion of element first check queue is full or not. If the queue is full then it generates an
error message “queue is full”.
2. Dequeue: This operation is deleting an element from the Front end of the queue. At the time of
deletion of element first check queue is empty or not. If the queue is empty then it generates an
error message “queue is empty”.

Representation and Implementation:


We have two representations of Queues. They are
1. Array Implementation
2. Linked list Implementation.
1. Array Implementation:
 In this implementation, queue is represented as a linear array.
 Every queue has associated with two variable known as Front and Rear
 Rear is the index where element to be inserted into queue and Front is the index where
element to be deleted from the queue.
 In this implementation every queue has maximum size i.e Max.
 If Front==0 and Rear == -1 or Front > Rear, indicates an empty queue and Rear == Max – 1
indicates that queue is full.

Algorithm of ENQUEUE:
1. Check if queue is full or not
2. If it is full then print an error message that queue is full and exit
3. If it is not full then increment Rear and insert element at Rear index.
Step 1: if Rear==Max-1 then print Queue is full and go to step 4.
Step 2: set Rear = Rear+1
Step 3: set Queue[Rear]=element
Step 4: exit

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 1


This enqueue operation is implemented by the following function in C language.
void Enqueue(int ele)
{
if(rear==MAX-1)
printf(“Queue is full”);
else
Queue[++rear]=ele;
}
Algorithm of DEQUEUE:
1. Check if the Queue is empty or not.
2. If it is empty then print an error message that Queue is empty and exit
3. If it is not empty then delete element at Front index and decrement Front.
Step 1: if rear==-1 or Front > Rear then print Queue is empty and goto step 4
Step 2: set element = Queue[Front]
Step 3: set Front = Front + 1
Step 4: exit.
This Dequeue operation is implemented by the following function in C language
void Dequeue()
{
if(rear==-1||front>rear)
printf(“Queue is empty”);
else
printf(“Dequeued element = %d”,Queue[front++];
}
2. Linked list Implementation:
 In this implementation, the queue is represented as linked list. i.e a linked list is used to
represent a queue.
 In this implementation, each element of the queue is stored as a node in the linked list.
 The main draw back in the array implementation is that array must have some fixed size.
Moreover we cannot insert the elements once queue is full.
 The key advantage of using a linked list for implementing a queue is that it provides dynamic
memory allocation which allows the queue to grow or shrink as needed without requiring a
fixed size like an array-based implementation.
 Each node contains two parts: the data (the value to be stored in the queue) and a pointer to the
next node. This node is implemented by the following structure in C,

struct Node {
int data;
struct Node* next;
};
typedef struct Node* NODE;
 The front pointer is the pointer to the first node of the linked list.
 The rear pointer is the pointer to the last node of the linked list.
 All the insertions are done at Rear end and all the deletions are done at Front end.
 If the front == NULL then it indicates that queue is empty.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 2


The enqueue operation is implemented by using the following function in C language
void Enqueue(int ele)
{
NODE *n;
n=(NODE *)malloc(sizeof(NODE));
n->data=ele;
n->next=NULL;
if(front==NULL)
{
front=n;
rear=n;
}
else
{
rear->next=n;
rear=n;
}
}
The dequeue operation is implemented by using the following function in C language
void dequeue()
{
if(front==NULL)
printf("Queue is empty");
else
{
printf("dequeued data is = %d", front->data);
front=front->next;
}
}
Applications of Queues:

In data structures, the First in First out property of queues makes them most useful in the
following applications

1. Breadth-First Search (BFS):

 This is a graph traversal method. It is also called Breadth First Traversal.


 The basic idea of this process is to start with any arbitrary vertex and mark it as visited.
 Then move to all adjacent unvisited nodes of it and mark them as visited.
 Check for any other unvisited adjacent nodes to visited node and traverse them.
 This process continues until all the nodes of graph can be marked as visited.

Breadth Fist Search Algorithm:


Step 1: Define a Queue with size of vertices in the graph.
Step 2: Select any vertex as starting vertex for traversal. Visit that vertex and insert it into
the Queue.
Step 3: Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue
and insert them into the Queue.
Step 4: When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
Step 5: Repeat steps 3 and 4 until queue becomes empty.
Step 6: Then the order of vertices deleted from the queue becomes BFS.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 3


2. Scheduling: Queues can be used to implement task scheduling algorithms. The scheduling
application of queues refers to the use of queues in managing tasks or jobs in various scheduling
systems. The following are some scheduling applications of queues

1. Task or Job Scheduling: Queues are widely used in systems where tasks or jobs need to be
scheduled for execution. In this context, a task could be any unit of work that needs to be
performed, such as running a program, processing data, or handling a request.

2. Priority-Based Scheduling: Queues can be used to implement priority-based scheduling


algorithms. Each task is associated with a priority, and tasks with higher priorities are
dequeued and executed before tasks with lower priorities.

3. Round-Robin Scheduling: Round-robin scheduling is a preemptive scheduling algorithm


commonly used in multitasking environments. In this approach, tasks are enqueued into a
queue, and each task is executed for a small time slice before being re-enqueued back into the
queue. This ensures fair CPU time allocation among tasks.

4. Real-Time Scheduling: Queues are essential in real-time scheduling systems, where tasks
have strict deadlines or timing requirements. Tasks are enqueued and dequeued based on their
deadlines or timing constraints to ensure that they are executed on time.

5. Batch Processing: In batch processing systems, queues are used to manage batches of tasks or
jobs. Tasks are enqueued into batches, and batches are processed sequentially to optimize
resource utilization and efficiency.

Circular Queue:
In normal linear queue, we can not insert element once queue becomes full, even we delete some
elements from the queue. This is because once rear position is moved to last index, it can not be
updated. This problem is overcome by circular queue. In this circular queue, the queue is organized
in circular manner, i.e the last element is followed by the first element in the queue.

Definition: A circular queue is an extended version of a normal queue where the last element of the
queue is connected to the first element of the queue forming a circle.

Operations on Circular Queue: The following operations that we can perform on these queues.

1. Enqueue: This operation is used to insert an element into queue. The element is always
inserted into from rear end.
2. Dequeue: This operation is used to delete an element from queue. This deletion always takes
from front end only.

Steps of Enqueue operation:


1. First, we check whether queue is full or not. If it is not full then only possible to insert element.
2. Initially the front and rear are set to -1. When we insert the first element, front and rear both set
to 0.
3. When we insert another new element, then rear gets incremented i.e rear = rear + 1

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 4


There are two cases in which element can’t be inserted,
1. When front==0 & rear = maxsize – 1, i.e front is at the first position of queue and rear at the
last position of the queue.
2. front == rear + 1

Algorithm:
Step 1: if(rear + 1)%max == front then
Print(“Queue is full”)
Step 2: if front == -1 and rear == -1 then
Set front = rear = 0
else if rear = max – 1 and front != 0 then
Set rear = 0;
else Set rear = (rear + 1) % max
Step 3: Set Queue[rear]= ele
Step 4: Exit

Steps of Dequeue operation:


1. First, we check whether queue is empty or not. If queue is empty then we can not perform this
operation.
2. When element is deleted, front gets decremented by -1.
3. If there is only one element which is to be deleted, then front and rear are reset to -1.

Algorithm:
Step 1: if front == -1 then
print(“Queue is empty”) goto step 4.
Step 2: Set ele = Queue[front].
Step 3: if front == rear then
Set front = rear = -1
else if front = max – 1 then
Set front =0
else Set front = front + 1
Step 4: Exit

Applications of Circular Queues:


 Memory management: The circular queue provides memory management. As we have
already seen that in linear queue, the memory is not managed very efficiently. But in case
of a circular queue, the memory is managed efficiently by placing the elements in a location
which is unused.
 CPU Scheduling: The operating system also uses the circular queue to insert the processes
and then execute them.
 Traffic system: In a computer-control traffic system, traffic light is one of the best examples of the
circular queue.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 5

You might also like