Chapter 5 Queue
Chapter 5 Queue
Queue
Data Structure
DATA STRUCTURE AND ALGORITHM 1
CONTENTS
Queue definition,
Operations on queue,
Queue implementation
Using array,
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
database implementations, and other areas.
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures.
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 − Add 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
remove 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.
Front = −1
0 1 2 3 4 Rear = −1
3. Array implementation of linear queues leads to the Queue_Full state even though the queue is not
actually full.
4. To avoid this, we 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.
0 1 2 3 4
Consider the following statements:
1. Enqueue (10)
2. Enqueue (20)
3. Enqueue (30)
4. Enqueue (40)
5. Dequeue ()
6. Dequeue ()
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. Dequeue ()
6. Dequeue ()
7. Enqueue (50)
8. Enqueue (60)
9. Enqueue (70)
10. Enqueue (80)
DATA STRUCTURE AND ALGORITHM Enqueue (80) will generate the message Queue_Full because 16
56 12 70 8
Rear
To execute both operations in constant time we have to maintain tail pointer in addition to head
pointer.
56 12 70 8
Elements can be inserted in any order in a priority queue, but when 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.
In this example, jobs are always removed from the front of the
queue.
The elements in the second queue are removed only when the
first queue is empty, and
The elements from the third queue are removed only when the
second queue is empty, and so on.
DATA STRUCTURE AND ALGORITHM 20
PRIORITY QUEUES IMPLEMENTATION
Implementation method 2
The second way 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.
The two ways to implement a priority queue are sorted list and unsorted list.
Sorted list: A sorted list is characterized by the following features:
Advantage—Deletion is easy; elements are stored by priority, so just delete from the beginning of the list.
Disadvantage—Insertion is hard; it is necessary to find the proper location for insertion.
A linked list is convenient for this implementation such as the list in Fig. 5.9.
Unsorted list: An unsorted list is characterized by the following features:
Advantage—Insertion is easy; just add elements at the end of the list.
Disadvantage—Deletion is hard; it is necessary to find the highest priority element first.
An array is convenient for this implementation.
Queues are also very useful in a time-sharing computer system where many 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, when the CPU 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 own 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.