0% found this document useful (0 votes)
31 views14 pages

Oop Unit 5

Uploaded by

vekariyajay985
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)
31 views14 pages

Oop Unit 5

Uploaded by

vekariyajay985
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/ 14

UNIT – 5:

Queue

Page 1 of 14
QUEUES
SIMPLE QUEUE
Another important type of lists allows deletion to be performed at one end of
list and insertion at the other end.
The information in such list is processed in the same order as it was
received. That is first-in first-out (FIFO) or a first-come first-serve (FCFS)
basis. This type of a list is known as Queue.
Definition: Queue is a linear non primitive data structure in which insertion
of an element occurs at one end and deletion occur at other end.

Representation of a queue
A queue can be represented by a vector as follow:
...
Insert
Delete
Front Rear
Fig: Representation of a queue by a Array

A vector representation of a queue requires two pointers f and r. f denotes


the positions of its front (leftmost) elements and r represents rear (rightmost)
elements.
EXAMPLES OF QUEUE
1. A checkout line at the supermarket cash register. Here the first person in
line will check out first.
2. The line of cars waiting at traffic signal. The first car at signal will be
deleted (move ahead) first. A car will be inserted (joining) at the end of
the line of existing waiting cars.
3. A queue in a time-sharing computer machine where many users share
the system simultaneously. Such a system has a single CPU and one
main memory. These resources must be shared. It allows one user’s
program to execute for a short time. Then execution of another user’s
program will be done and so on. This will be done until it returns to the

Page 2 of 14
execution of the initial user’s program. The user programs that are
waiting in a waiting queue may be processed based on FIFO.
CHARACTERISTICS
A queue data structure has following characteristics:
 Its nature is FIFO (First In First Out).
 The insertion and deletion operation occur at opposite end.

OPERATIONS ON QUEUE
Following operations can be performed on Queue.
1. Insert Used to insert an element in a queue.
2. Delete Used to delete an element from a queue.

1. QINSERT
Q[5] a queue containing 5 elements
Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue
Data Data to be inserted at the rear of queue

1. [Overflow?]
If (Rear == Size -1) then
Write (‘OVERFLOW’)
Return
End if
2. [Increment rear pointer]
Rear= Rear + 1
3. [Insert element]
Q [Rear] = Data
4. [Is front pointer properly set?]
If (Front == -1) then
Front = -1
Return
End if

Page 3 of 14
2. QDELETE ()
This function will delete an element from the front of queue. And returns this
element.
Q[5] a queue containing 5 elements
Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue

1. [Underflow?]
If Front == -1 then
Write (‘ UNDERFLOW’)
Return
End if
2. [Delete element]
Print (Q [Front])
Q[Front]=null
3. [Increment in Front Pointer]
If Front = Rear then
Front = Rear = -1
else
Front = Front + 1
End if
4. Exit

Trace of operations on a simple queue

B C Delete A
Consider an example w Empty

f r f r
A Insert A C Delete B

f r f r

A B Insert B C D Insert D

f r f r

C D Insert E
A B C Insert C

f r f r ( overflow)

Page 4 of 14
Here the size of the queue is four elements. Initially, the queue is empty. It is
required to insert symbols ‘A’,’B’ and ‘C’, delete ‘A’ and ‘B’ and insert ‘D’ and
‘E’. A trace of the contents of the queue is given in following figure. Note that
an overflow occurs on trying to insert symbol ‘E’, even though the first two
locations are not being used. This is limitation in simple queue and its lead
to wastage of memory / storage. This limitation will be overcome in circular
queue.

CIRCULAR QUEUE
Circular queue is more suitable method of representing a queue. It stops an
unnecessary use of memory. It arranges the elements Q[1],Q[2],…,Q[n] in a
circular fashion with Q[1] following Q[n].

Representation of a circular queue

.
R

. Q[3]

.
Q[2]

Q[1]
Q[N-1]
F
Q[N]

Definition: A queue in which the elements are arranged in the circular


fashion is known as Circular queue.

OPERATIONS ON QUEUE
Following operations can be performed on Queue.
3. Insert Used to insert an element in a queue.
4. Delete Used to delete an element from a queue.

Page 5 of 14
2. Circular Queue INSERT
This function inserts element Y at the rear of queue.

Q[5] a queue containing 5 elements


Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue
Data Data to be inserted at the rear of queue

1. [Overflow?]
If ((Front==0 and rear==size-1) Or (front - rear==1)) then
Write (‘OVERFLOW’)
Return
End if
2. [Increment rear pointer]
IF (rear == size -1) then
Rear=0
Else
Rear= Rear + 1
3. [Insert element]
Q [Rear] = Data
4. [Is front pointer properly set?]
If (Front == -1) then
Front = -1
Return
End if

Page 6 of 14
2. QDELETE ()
This function will delete an element from the front of queue. And returns this
element.
Q[5] a queue containing 5 elements
Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue

1. [Underflow?]
If Front == -1 then
Write (‘ UNDERFLOW’)
Return
End if
2. [Delete element]
Print (Q [Front])
Q[Front]=null
3. [Increment in Front Pointer]
If Front == Rear then
Front = Rear = -1
Else if (Front == Size -1)
Front=0
else
Front = Front + 1
End if
4. Exit

DOUBLE-ENDED QUEUE / DEQUE


It is a linear list in which insertions and deletions are done at both end of the
structure. It is also known as Deque.
Representation of a doubly ended queue
A double ended queue can be represented by a vector as follow:
Insert Delete

...
Delete Insert

Page 7 of 14
There are two types of a deque:
1. Input-restricted deque: The insertion operation is performed at only
one end and deletion operation is performed at both the ends.

i) Insert Right (Increment Rear)


ii) Delete Left (Increment Front)
iii) Delete Right (Decrement Rear)

2. Output-restricted deque: The deletion operation is performed at only


one end and insertion operation is performed at both the ends.

i) Insert Right (Increment Rear)


ii) Insert Left (Decrement Front)
iii) Delete Left (Increment Rear)

INSERT RIGHT (Default Insertion)


This function inserts element at the rear of queue.

Q[5] a queue containing 5 elements


Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue
Data Data to be inserted at the rear of queue

Page 8 of 14
1. [Overflow?]
If (Rear == Size -1) then
Write ("OVERFLOW A RIGHT SIDE"’)
Return
End if
2. [Increment rear pointer]
Rear= Rear + 1
3. [Insert element]
Q [Rear] = Data
4. [Is front pointer properly set?]
If (Front == -1) then
Front = 0
Return
End if

INSERT LEFT

Q[5] a queue containing 5 elements


Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue
Data Data to be inserted at the rear of queue

1. [Overflow?]
If (Front==0) then
Write (‘Overflow at Left SIde’)
Return
End if
2. [decrement Front pointer]
IF (front == -1) then
Front = Rear = Size -1
Else
Front= Front - 1
End if
3. [Insert element]
Q [Front] = Data

Page 9 of 14
DELETE LEFT (Default Deletion)
This function will delete an element from the front of queue. And returns this
element.
Q[5] a queue containing 5 elements
Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue

1. [Underflow?]
If Front == -1 then
Write (‘ UNDERFLOW’)
Return
End if
2. [Delete element]
Print (Q [Front])
Q[Front]=null
3. [Increment Front Pointer]
If Front == Rear then
Front = Rear = -1
else
Front = Front + 1
End if
4. Exit

DELETE RIGHT
This function will delete an element from the front of queue. And returns this
element.
Q[5] a queue containing 5 elements
Front= -1 a pointer to front element of a queue
Rear= -1 a pointer to rear element of a queue

1. [Underflow?]
If Rear == -1 then
Write (‘ Underflow at Right Side’)
Return
End if
2. [Delete element]
Print (Q [Rear])
Q[Rear]=null
3. [Decrement Rear Pointer]
If Front == Rear then
Front = Rear = -1
else
Rear = Rear - 1
End if

Page 10 of 14
4. Exit

Note: DQueue allows deletion from left as well as right side simultaneously.
But that is not possible in insertion. Once insertion is start from right it will
not allow to insert left side until entire queue is empty and vice a versa.

Priority Queue (Definition)


A priority queue is a collection of elements such that each element has been
assigned a priority and the order in which elements are deleted and
processed with following rules:
 An element of higher priority is processed before any element of lower
priority.
 Two elements with same priority are processed according to order in
which they are added to the queue.

Applications of Priority Queue:


1) CPU Scheduling
2) Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum
Spanning Tree, etc

Two Types of Priority Queue


1) Ascending Priority Queue: An Element can be inserted in any
order, but while deleting an element from queue it start from smallest
priority number to largest priority number.
2) Descending Priority Queue: An Element can be inserted in any
order, but while deleting an element from queue it start from largest
priority number to smallest priority number.

Applications of Queue:

Queue is used when things don’t have to be processed immediately, but


have to be processed in First In First Out order like Breadth First Search.
This property of Queue makes it also useful in following kind of scenarios.

a. When a resource is shared among multiple consumers. Examples


include CPU scheduling, Disk Scheduling.

b. When data is transferred asynchronously (data not necessarily


received at same rate as sent) between two processes. Examples
include IO Buffers, pipes, file IO, etc

Page 11 of 14
c. In print spooling, documents are loaded into a buffer and then the
printer pulls them off the buffer at its own rate. Spooling also lets you
place a number of print jobs on a queue instead of waiting for each
one to finish before specifying the next one.
d. Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive, First come first served.

e. In real life, Call Center phone systems will use Queues, to hold
people calling them in an order, until a service representative is free.

f. Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax
booth leaves the booth first. The vehicle that comes last leaves last.
Therefore, it follows first-in-first-out (FIFO) strategy of queue.

What will be the position of front and rear if circular queue is full?
Case 1 : FRONT == 0 && REAR == SIZE -1
Case 2: FRONT – REAR == 1

What is simulation?
Simulation is the process of forming an abstract model from a real
situation in order to understand the impact of modifications and the effect of
introducing various strategies and inputs on the situation.

Acting out or mimicking an actual or probable real life condition, event, or


situation to find a cause of a past occurrence (such as an accident), or to
forecast future effects (outcomes) of assumed circumstances or factors. A
simulation may be performed through
1) solving a set of equations (a mathematical model),
2) constructing a physical (scale) model,
3) staged rehearsal,
4) game (such as war games), or a computer graphics model (such as an
animated flowchart). Whereas simulations are very useful tools that allow
experimentation without exposure to risk, they are gross simplifications of
the reality because they include only a few of the real-world factors, and are
only as good as their underlying assumptions.

Page 12 of 14
Difference / Comparison between Stack and Queue

BASIS FOR
Sr STACK QUEUE
COMPARISON
LIFO (Last in First
1 Working principle FIFO (First in First out)
out)
One end is used for insertion,
Same end is used
i.e., rear end and another end
2 Structure to insert and delete
is used for deletion of
elements.
elements, i.e., front end.
Number of
3 One Two (In simple queue case)
pointers used
Operations
4 Push and Pop Insert and Delete
performed
Examination of
5 Top == -1 Front == -1 || Rear ==-1
empty condition
Examination of
6 Top == Size - 1 Rear == Size - 1
full condition
It has variants like circular
It does not have
7 Variants queue, priority queue, doubly
variants.
ended queue.
8 Implementation Simpler Comparatively complex

What is FIFO & LIFO?


FIFO: Stands for "First In, First Out." FIFO is a method of processing and
retrieving data. In a FIFO system, the first items entered are the first ones to
be removed. In other words, the items are removed in the same order they
are entered.

Computers often implement the FIFO system when extracting data from an
array or buffer. If the first data entered into the buffer must be extracted first,
the FIFO method is used. The opposite of FIFO is LIFO, in which the last
data entered is the first to be removed.

LIFO: Stands for "Last In, First Out." LIFO is a method of processing data in
which the last items entered are the first to be removed. This is the opposite
of LIFO is FIFO (First In, First Out), in which items are removed in the order
they have been entered.

Page 13 of 14
To better understand LIFO, imagine stacking a deck of cards by placing one
card on top of the other, starting from the bottom. Once the deck has been
fully stacked, you begin to remove the cards, starting from the top. This
process is an example of the LIFO method, because the last cards to be
placed on the deck are the first ones to be removed.

The LIFO method is sometimes used by computers when extracting data


from an array or data buffer. When a program needs to access the most
recent information entered, it will use the LIFO method. When information
needs to be retrieved in the order it was entered, the FIFO method is used.

Difference Between Simple (Linear) Queue and Circular Queue and


justify which is better?

Sr Simple (Linear) Queue Circular Queue

The linear queue is an ordered


list in which data elements are circular queue stores the data in the
1
organized in the sequential circular fashion.
order.
The insertion and deletion of
the elements is fixed in linear the circular queue is capable of
2 queue i.e, addition from the inserting and deleting the element
rear end and deletion from the from any point until it is unoccupied.
front end
Linear queue wastes the circular queue makes the efficient
3
memory space use of space.

Conclusion
The linear queue is inefficient in certain cases where the elements are
required to shift to the vacant spaces in order to perform insertion operation.
That is the reason it tends to waste the storage space while circular queue
makes appropriate use of storage space as the elements are added at any
position if there exists an empty space.

Page 14 of 14

You might also like