Oop Unit 5
Oop 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
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
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].
.
R
. Q[3]
.
Q[2]
Q[1]
Q[N-1]
F
Q[N]
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.
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
...
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.
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
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.
Applications of Queue:
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.
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
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.
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