R23 - DS - Unit IV-1
R23 - DS - Unit IV-1
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”.
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
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.
In data structures, the First in First out property of queues makes them most useful in the
following applications
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.
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.
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
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