1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
5. QUEUE
2Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objectives Learn about restricted linear list, the ‘Queue’ Implement Queue using arrays Learn and implement the Circular Queue Study use of Queue in simulations
3Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The Concept A queue is an ordered list in which all insertions
are done at one end, called rear and deletions at another end called front
Queue when implemented using arrays has some drawbacks which can be avoided by circular queue
Queue is used in many applications such as simulation, priority queue, job queue etc
4Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Queue One of the most common data processing
structures Frequently used in most of the system software's
like operating systems, Network and Database implementations and in many more other areas
Very useful in time-sharing and distributed computer systems where many widely distributed users share the system simultaneously
5Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Queue As An Adt
Create : Create creates an empty queue, Q Add (i,Q) : Add adds the element i to the
rear end of queue, Q and returns the new queue
Delete (Q) : Delete takes out an element i from the front end of queue and returns the resulting queue
Front(Q) : Front returns the element i which is at the front position of queue
Is_Empty_Q(Q) : Is_Empty_Q returns true if queue is empty otherwise returns false
6Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Drawbacks of linear queue using array
The linear queue is of fixed size. So the user does not have flexibility to dynamically change the size of the queue
Arbitrarily declared maximum size of queue leads to poor utilization of memory
We need to write suitable code to make Front regularly catch up with Rear and resetting of both queue Front and Rear. It leads to queue full state even though actually queue is not full
To avoid this, when queue full is signaled, we need to rewind the entire queue to the original start location (if there are vacant empty locations) so that the first element is at 0th location and Front is set to –1. Such movement of data is efficient way to avoid this drawback.
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Circular Queue
Fig 1: The queue is said to be full only when there are n
elements in the queue.
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
If more number of queues is required to be implemented, then efficient data structure to handle multiple queues is required
It is possible to utilize all the available space in a single array
Multi-queue
9Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The word deque is a short form of double-ended queue and defines a data structure in which items can be added or deleted at either the front end or rear end, but no changes can be made else where in the list. It is pronounced as ‘deck’
Thus a deque is a generalization of both a stack and a queue
It is sequential container that is optimized for fast index-bccess and efficient insertion at either of its ends.
Deque
10Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Following figure shows the representation of a deque
Fig 2: Representation of a deque
11Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Priority queue is a collection of finite number of prioritized items
Priority queues are the queues in which we can insert items or delete items from any position based on some fundamental ordering of the elements
Priority queue is a collection of elements where the elements are stored according to their priority levels. The order in which the elements should get added or removed is decided by the priority of the element
Priority Queue
12Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The element with a higher priority is processed before any element of lower priority
If there were elements with the same priority, then the element added first in the queue would get processed first
Priority queues are used for implementing job scheduling by the operating system where jobs with higher priority are to be processed first.
Another application of priority queues is simulation systems where priority corresponds to event times
There are two ways to implement priority queue
Following rules are applied to maintaina priority queue:
13Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
An example of a priority queue is a list of patients in an emergency room; each patient might be given a ranking that depends on the severity of the patient’s illness
Another example of priority queue is a list of background jobs on a multi-user computer; each background job is given a priority level
Common applications
14Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Initialize PQ to be the empty priority queue Determine if PQ is empty Determine if PQ is full. If PQ is not full, insert an item X into PQ If PQ is not empty, remove an item X of the
highest priority
Operations on a Priority Queue, Priority :
15Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Structure for priority queue as follows :
struct priority queue{
int data;int priority;
}
16Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
After inserting 81 with priority 3, the updated queue is as follows :
17Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Hence two ways to Implement a Priority Queue :
Sorted list a) Advantage : Deletion is easy; items are stored by
priority, so just delete from the beginning of the list b) Disadvantage : Insertion is hard; it is necessary to
find the proper location for insertion c) A linked list is convenient for this implementation
Unsorted list a) Advantage : Insertion is easy; just add item to the end
of the list b) Disadvantage : Deletion is hard; it is necessary to find
the item first c) An array is convenient for this implementation
18Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Array Implementation of a Priority Queue
where data Type would be data type of item like int, char, etc.
Priority would be the priority number of the element and order would be the priority number of the element in which the element has been added to the queue
struct data{<data Type> item;int priority;int order;} PQ[20];
19Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Queue Applications
Josephus Problem Job Scheduling Simulation
20Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Queue Applications
A powerful tool that can be used to study the behavior of systems is simulation
“Simulation is the process of forming an abstract model of a real world in order to understand the effect of modifications and the effect of introducing various strategies on the situation”
21Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
End of
Chapter 5 …!