0% found this document useful (0 votes)
20 views10 pages

Unit-4 Queue

The document provides an overview of queues, a data structure where items are added at the rear and removed from the front, following the FIFO principle. It discusses various types of queues including linear, circular, dequeue, and priority queues, along with their algorithms for insertion and deletion. Additionally, it highlights real-life applications and simulations related to queue usage in computer science.

Uploaded by

espinozaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Unit-4 Queue

The document provides an overview of queues, a data structure where items are added at the rear and removed from the front, following the FIFO principle. It discusses various types of queues including linear, circular, dequeue, and priority queues, along with their algorithms for insertion and deletion. Additionally, it highlights real-life applications and simulations related to queue usage in computer science.

Uploaded by

espinozaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structure UNIT: QUEUES By Dr.

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.

Front: Remove ONLY


Rear: Add ONLY

 Queue is called FIFO because in this type of data structure whichever item is inserted first will be
deleted first.

Real life examples:

1) A waiting line in a store, at a service counter, on a one-lane road


2) The railway reservation counter where the people collect their tickets on the first come first out
basis.
3) Time sharing system where many users share the system simultaneously.

Applications related to Computer Science:

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)

BMU- BMCCA Page 1 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

➢ Types of Queue:

1. Simple or Linear Queue:-

In the linear queue it has two pointers:


1. Rear: If we want to insert the element in the queue, you have to increase rear pointer.
2. Front: If we want to delete or remove the element from the queue, you have to increase front
pointer.

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

Consider the example of queue which has 5 elements A, B, C, D, and E


0 1 2 3 4
2
A B C D E

F R

Now if we delete the 2 elements A & B than the queue will be

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.

Algorithm of Insert into linear queue

Step-1 [Check for overflow]


If (rear >= size - 1) then
Print “Overflow”
Else go to step-2
Step-2 [Increment the rear pointer so that it points at the location where to insert an element]

BMU- BMCCA Page 2 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

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

Algorithm of Delete an element into linear queue

Step-1 [Check for underflow]


If (front = -1) then
Print “underflow”
return
Else goto step-2
Step-2 [Extract the value from the queue at location of front pointer]
value = q[front]
Step-3 [Set the front pointer in queue]
If (front = = rear) then
front = rear = -1
else
Step-4 [Increment the front pointer]
front = front +1
Step-5 return value

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]

BMU- BMCCA Page 3 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

 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

Algorithm for Inserting an element into circular queue

Step-1 [Check for overflow]


If ((rear >= size - 1 && front = 0) || (front = rear + 1) then
Print “Overflow”
return
Else go to step-2
Step-2 [Not pointing at first node & Insert the element at the location of rear]
If (rear >= size -1) then
rear = 0
else goto step-3
Step-3 [Increment the rear pointer so that it points at the location where to insert an element]
rear = rear + 1
Step-4 [Insert the element at the location of rear]
q [rear] = value
Step-5 [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-6 exit

BMU- BMCCA Page 4 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

Algorithm for Deleting an element from circular queue

Step-1 [Check for underflow]


If (front = -1) then
Print “underflow”
return
Else goto step-2
Step-2 [Extract the value from the queue at location of front pointer]
value = q[front]
Step-3 [Set the front pointer in queue]
If (front = = rear) then
front = rear = -1
else if (front = size -1) then
front = 0
else
Step-4 [Increment the front pointer]
front = front +1
Step-5 return (value) & exit

3. Dequeue(Double Ended Queue):-

 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

BMU- BMCCA Page 5 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

1. Input Restricted Dequeue


2.
A dequeue which allow insertion at only one end of the queue but allow deletion from either
side is know as “input restricted dequeue”.
Right Note:-
left side
side Insertion Insertion:
Right= + (R)
Deletion Deletion Deletion:
Left= + (F)
R Right= - (R)
F
3. Output Restricted Dequeue
A dequeue which allow deletion from only one end of the queue but allow insertion at either
side is know as “input restricted dequeue”.
Note:-
Right
left side Deletion:
side
Insertion Left= + (F)
Insertion Insertion:
Deletion
Left= - (F)
Right= + (R)
F R
Algorithm of Insert into Dequeue queue (through Right end)

Step-1 [Check for overflow]

If (rear >= size-1) then

Print “Overflow queue from right end”

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

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

BMU- BMCCA Page 6 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

Algorithm of Delete an element into Dequeue queue (through Left end)

Step-1 [Check for underflow]


If (front = -1) then
Print “underflow queue from left end”
return
Else go to step-2
Step-2 [Extract the value from the queue at location of front pointer]
value = q[front]
Step-3 [Set the front pointer in queue]
If (front = rear) then
front = rear = -1
else
Step-4 [Increment the front pointer]
front = front +1
Step-5 return value

Algorithm of Insert into Dequeue queue (through Left end)

Step-1 [Check for overflow]


If (front = 0) then
Print “Overflow queue from left end”
else go to step-2
Step-2 [Decrement the front pointer so that it points at the location where to insert an element]
If (front = = -1) then
front = rear = size – 1
else
front = front – 1
Step-3 [Insert the element at the location of front]
q [front] = value
Step-4 exit

BMU- BMCCA Page 7 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

Algorithm of Delete an element into Dequeue queue (through Right end)

Step-1 [Check for underflow]


If ( rear = -1) then
Print “underflow Queue from right side”
return
Else goto step-2
Step-2 [Extract the value from the queue at location of rear pointer]
value = q[rear]
Step-3 [Set the rear & front pointer in queue]
If (front = rear) then
front = rear = -1
else
Step-4 [Decrement the rear pointer]
rear = rear - 1
Step-5 return (value) & 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

BMU- BMCCA Page 8 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

 There are two types of priority queue:

(1) Ascending Priority Queue: -


It is a collection of items into which item can be inserted arbitrarily & from which
only the smallest item can be removed.

(2) Descending Priority Queue: -


It is similar but allows deletion of only the largest item.

➢ Application of Queue: -

There are two types of queue application


1) Simulation
2) Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
3) In real life, Call Center phone systems will use Queues, to hold people calling them in an order,
until a service representative is free.
4) Handling of interrupts in real-time systems. The interrupts are handled in the same order as they
arrive, First come first served.
1. Simulation:
 Simulation is the process of forming an abstract model from a real situation in order to
understand the impact of modifications & the effect of introducing various strategies on the
situation.
 It allows the user to experiment with real & proposed situations otherwise impossible or
impractical.
 The major advantage of simulation is that it permits experimentation without modifying the
real situation. Areas such as military operations are safer to simulate then to field test.
 Continuous System has parameters which can take any real value in some given interval.
Simulations of continuous systems operate on these continuous parameters. The solar system
is the example of continuous system.
 Discrete system has parameters which can only take values from a fixed number of choices.
Blender is an example of discrete system.
 A system can also be either deterministic or stochastic depending on the relationship between
its input & output.
• Deterministic System is that where the final outcome can be predicated if the initial state
& input to the system are known. e.g. keyboard
• Stochastic System is that where you can not predict the output means there is no known
explanation for its randomness. The feather is dropped of a building; it rarely lands in the
same spot, since it is affected by many unpredictable factors.

BMU- BMCCA Page 9 of 10


Data Structure UNIT: QUEUES By Dr. Sneha Patel

 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

Q: 2 Long Question Answer (Marks: 5 or 7 or 10)

(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

You might also like