Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Learn about restricted linear list, the ‘Queue’
 Implement Queue using arrays
 Learn and implement the Circular Queue
 Study use of Queue in simulations
2
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
3
 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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 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
4
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 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
5
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
6
 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.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
7
Fig 1: The queue is said to be full only when there are n
elements in the queue.
Oxford University Press © 2012Data 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
8
Multi-queue
Oxford University Press © 2012Data 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.
9
Deque
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
10
Following figure shows the representation of a deque
Fig 2: Representation of a deque
Oxford University Press © 2012Data 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
11
Priority Queue
Oxford University Press © 2012Data 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
12
Following rules are applied to maintaina
priority queue:
Oxford University Press © 2012Data 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
13
Common applications
Oxford University Press © 2012Data 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
14
Operations on a Priority Queue, Priority :
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
15
Structure for priority queue as follows :
struct priority queue
{
int data;
int priority;
}
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
16
After inserting 81 with priority 3, the updated queue is as
follows :
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
17
 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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 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
18
struct data
{
<data Type> item;
int priority;
int order;
} PQ[20];
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Josephus Problem
 Job Scheduling
 Simulation
19
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 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”
20
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
End
of
Chapter 5 …!
21

5. Queue - Data Structures using C++ by Varsha Patil

  • 1.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 1
  • 2.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  Learn about restricted linear list, the ‘Queue’  Implement Queue using arrays  Learn and implement the Circular Queue  Study use of Queue in simulations 2
  • 3.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 3  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
  • 4.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  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 4
  • 5.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  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 5
  • 6.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 6  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.
  • 7.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 7 Fig 1: The queue is said to be full only when there are n elements in the queue.
  • 8.
    Oxford University Press© 2012Data 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 8 Multi-queue
  • 9.
    Oxford University Press© 2012Data 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. 9 Deque
  • 10.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 10 Following figure shows the representation of a deque Fig 2: Representation of a deque
  • 11.
    Oxford University Press© 2012Data 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 11 Priority Queue
  • 12.
    Oxford University Press© 2012Data 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 12 Following rules are applied to maintaina priority queue:
  • 13.
    Oxford University Press© 2012Data 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 13 Common applications
  • 14.
    Oxford University Press© 2012Data 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 14 Operations on a Priority Queue, Priority :
  • 15.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 15 Structure for priority queue as follows : struct priority queue { int data; int priority; }
  • 16.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 16 After inserting 81 with priority 3, the updated queue is as follows :
  • 17.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 17  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
  • 18.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  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 18 struct data { <data Type> item; int priority; int order; } PQ[20];
  • 19.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  Josephus Problem  Job Scheduling  Simulation 19
  • 20.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  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” 20
  • 21.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil End of Chapter 5 …! 21