DSA Chapter 5 - Queue
DSA Chapter 5 - Queue
DSA Chapter 5 - Queue
D A T A STRUCTURES A N D ALGORITHMS
Queue
Data
DATA STRUCTURE A N D ALGORITHM
Structure 1
CONTENTS
◾ Queue definition,
◾ Operations on queue,
◾ Queue implementation
◾ Using array,
◾ Using linked lists,
◾ Circular queue,
◾ Priority queue
◾ Applications,
◾ A queue is a list in which insertions are permitted only at one end of the list called its
rear/tail, and all deletions are constrained to the other end called the front/head of the queue.
◾ One end is always used to insert data (enqueue) and the other is used to remove data (dequeue).
◾ Real-world examples,
◾ They are frequently used in most system software such as operating systems, network and
◾ As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and
Structures.
DATA STRUCTURE A N D ALGORITHM 4
5.2. O P E R A T I O N S O N Q U E U E
Basic Operations
◾ enqueue() − add (store) an item to the queue.
◾ dequeue() − remove (access) an item from the queue.
Supportive operations
◾ getFront() − Gets the element at the front of the queue without removing
it.
◾ isfull() − Checks if the queue is full.
◾ isempty() − Checks if the queue is empty.
◾ The following steps should be taken to enqueue (insert) data into a queue −
◾ Step 3 − If the queue is not full, increment rear pointer to point the next empty
space.
◾ Step 4 − A dd data element to the queue location, where the rear is pointing.
◾ Accessing data from the queue is a process of two tasks − access the data where front is pointing
and re m ove the data after access.
◾ Step 3 − If the queue is not empty, increment front pointer and access the data where front is pointing.
◾ Is_Empty: This operation checks whether the queue is empty or not.This is confirmed by comparing
the values of Front and
Rear.
◾ Is_Full: When Rear points to the last location of the array, it indicates that the queue is
full
◾ Add: This operation adds an element in the queue if it is not full. As Rear points to the last element
of
the queue, the new element is added at the (rear + 1)th location.
◾ Delete: This operation deletes an element from the front of the queue and sets Front to point to the next
element. We should first increment the value of Front and then remove the element.
◾ getFront: returns the element at the front, but unlike delete, this does not update the value of
Front.
3. Array implementation of linear queues leads to the Queue_Full state even though the queue is
not actually full.
4. To avoid this, w e need to move the entire queue to the original start location (if there are
empty
locations) so that the first element is at the 0th location and Front is set to -1 (rear= rear-1).
This is obviously not a feasible solution as it is time consuming and involves a lot of data
movement. This
becomes impractical, especially when the queue is of a large size.
DATA STRUCTURE A N D ALGORITHM 13
CIRCULAR QUEUE
◾ Allows the queue to wraparound upon reaching the end of the array eliminates drawbacks
of linear queue.
◾ As w e go on adding elements to the queue and reach the end of the array, the next
element is
stored in the first slot of the array if it is empty.
◾ The queue is said to be full only w he n there are n elements in the queue.
0 1 2 3 4
Consider the following
statements:
1. Enqueue (10)
2. Enqueue (20)
3. Enqueue (30)
4. Enqueue (40)
5. Dequeue ()
6. D equeue ()
7. Enqueue (50)
8. Enqueue (60)
9. Enqueue (70)
10.Enqueue (80)
0 1 2 3 4
Consider the following
statements:
1. Enqueue (10)
2. Enqueue (20)
3. Enqueue (30)
4. Enqueue (40)
5. D equeue ()
6. Dequeue ()
7. Enqueue (50)
8. Enqueue (60)
9. Enqueue (70)
10.Enqueue (80)
DATA STRUCTURE A N D ALGORITHM Enqueue (80) will generate the message Queue_Full because 16
56 12 70 8
Rear
To execute both operations in constant time w e have to maintain tail pointer in addition to
head pointer.
56 12 70
◾ Elements can be inserted in any order in a priority queue, but w he n an element is removed
from the priority queue, it is always the one with the highest priority.
1. The element with a higher priority is processed before any element of lower priority.
2. If there were elements with the same priority, then the element added first in the queue would
get processed first.
Implementation method 1
◾ Implemented using three separate queues, each following the FIFO behavior strictly as shown
in bellow.
The elements from the third queue are removed only when
the
second queue is empty, and so on.
DATA STRUCTURE A N D ALGORITHM 20
PRIORITY QUEUES IMPLEMENTATION
Implementation method 2
◾ The second w a y of priority queue implementation is by using a structure for a queue.
Struct QueueElement
{
int Data;
int priority;
};
◾ The highest priority element is at the front and that of the lowest priority is at the
rear.
DATA STRUCTURE A N D ALGORITHM 21
CONT..
◾The two ways to implement a priority queue are sorted list and unsorted
◾ Queues are also very useful in a time-sharing computer system where m a n y users share a
system simultaneously.
◾ Whenever a user requests the system to run a particular program, the operating system adds
the request at the end of the queue of jobs waiting to be executed.
◾ Now, w he n the C P U is free, it executes the job that is at the front of the job queue.
◾ Similarly, there are queues for shared I/O devices too. Each device maintains its o w n queue
of
requests.
◾ Queues are also used in simulations and different problem solving operations. Read the
reference book for more information on applications of queues.
DATA STRUCTURE A N D ALGORITHM 23