Unit-4 Queue
Unit-4 Queue
Sneha Patel
Queue: -
Queue is an order collection items from which items may be deleted at one end called front of the
queue & into which items may be inserted at other end called rear of the queue.
Queue is called FIFO because in this type of data structure whichever item is inserted first will be
deleted first.
1) Job Scheduling (e.g. Round-Robin algorithm for CPU allocation- Equal-priority processes waiting
to run on a processor in a computer system)
2) Task scheduling in operating systems
3) Data transfer in network communication
4) Simulation of real-world systems (e.g., waiting lines)
5) Priority queues for event processing queues for event processing
6) An electronic mailbox is a queue
✓ The ordering is chronological (by arrival time)
➢ Types of Queue:
Exit Entry
Deletion F R Insertion
There are certain limitations with the linear queue that the algorithm of this queue may have wastage
of storage space
F R
0 1 2 3 4
2
C D E
F R
If we want to insert an element that it would not allowed you according to this algorithm since the rear
pointer is equal to 4 even though the 1st 2 element are empty in queue. So to overcome this problem
we have to construct the circular queue.
rear = rear + 1
Step-3 [Insert the element at the location of rear]
q [rear] = value
Step-4 [set the front pointer at the new element (if it is the first element added in the queue)]
If (front = -1) then
front = 0
Step-5 exit
2. Circular Queue:-
To overcome the problem of liner queue will construct circular queue. In this queue after queue of
N Q[n] comes Q[0].
In other words we can say that queue is called circular queue when last room comes just before the
first room.
[Circular Queue]
In circular queue when rear=size-1 if we want to insert element than this element will be assigned
to Q[0] that is instead of incrementing rear pointer we will reset rear pointer to 0.
Similarly if front = size-1 & one element is deleted we will reset front pointer to 0.
For example, a circular has 5 elements which are A, B, C, D, E & than we will remove two
element & at last we insert other 2 elements. This is the sequence of circular queue.
0 1 2 3 4 0 1 2 3 4
2 2
A B C D E C D E
F R F R
[Insert Element] [Delete Element]
0 1 2 3 4
2
F G C D E [After insert two elements]
R F
Dequeue is a linear list in which insertion & deletion are made from end of queue but not in
middle.
If you remove only from one side it is “stack”.
If you remove from one side & add to other side it is “queue”.
If you remove & add from both sides it is “dequeue”.
There are two types of dequeue:
1. Input restricted dequeue
2. Output restricted dequeue
Else go to step-2
Step-2 [Increment the rear pointer so that it points at the location where to insert an element]
rear = rear + 1
q [rear] = value
Step-4 [set the front pointer at the new element (if it is the first element added in the queue)]
front = 0
Step-5 exit
4. Priority Queue:-
A queue in which is possible to insert an element or remove an element at any position depending
upon some priority is called priority queue.
Application of priority queue:-
Let’s take a priority queue of job waiting to use a printer on LAN system. Priorities of 1, 2, 3 & 4
have been attached to every job. Administrator job has 1st priority. Teachers have 2nd priority.
Students have 3rd priority & outside users have 4th priority. Therefore if a job is indicated with
priority key it is inserted immediately at the end of the queue. Of other job with same priority key
here p is 1, 2, 3, & 4 in this queue. Jobs are always deleted from front of the queue. In general, we
can say that a priority queue is a series of queues representing situation in which it is known as
priority what priorities are associated with queue element.
1 1 1 2 2 3 3 3
S1 S2 Si-1 T1 Tj-1 O1 O2 Ok-1
F R
➢ Application of Queue: -
Time Driven Simulation: A main clock is used to keep track of simulation time. The clock is
allowed to increase one unit at a time. After each increase the program must examine each
event to determine, if it is to occur at this time. If the event is to be processed, the activity
associated with the event is performed. This approach is often used in continuous simulations
& where many events may occur at once.
Even Driven Simulation: program in this case must examine all events in the model to
determine which is to happen next. In order to accomplish this, each event must have its own
clock to keep track of the next time it will occur. After the main clock is updated, the next
activity associated with the event is performed & the event clock set to the next occurrence of
the event. This method is mainly used in discrete simulation.
UNIT: - Queue
Q: 1 Short Question Answer (Marks-1)
(1) Define the position of front and rear if circular queue is full?
(2) Define position of front and rear if circular queue is empty?
(3) List out types of queues.
(4) Define simulation.
(5) Define priority queue.
(6) Which condition is necessary for overflow in circular queue?
(7) List out various applications of queue.
(8) A linear list of elements in which deletion can be done from one end (front) and insertion can take
place only at the other end (rear) is known as _____________
a) Queue b) Stack c) Tree d) Linked list
(9) A queue follows __________
a) FIFO b) LIFO c) Ordered array d) Linear tree
(10) A data structure in which elements can be inserted or deleted at/from both ends but not in the
middle…
a) Queue b) Circular queue c) Dequeue d) Priority queue
(1) What is double ended queue? Explain difference between input restricted & output restricted
D-queue.
OR
Explain the Dequeue. Explain its operations.
(2) Short note on: difference between stack & queue.
OR
Explain the concept of queue. Compare stack and queue.
(3) Write an algorithm for Liner queue or Simple queue.
(4) Explain circular queue operation. (in this question write down algorithm for Circular queue)
(5) What is queue? Explain difference between simple and circular queue.
(6) Explain simulation with example.
BMU- BMCCA Page 10 of 10