Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views17 pages

Queue 2. Operations On Queue

Queue Opearations

Uploaded by

rishabhjangid33
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)
10 views17 pages

Queue 2. Operations On Queue

Queue Opearations

Uploaded by

rishabhjangid33
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/ 17

Queue

1. A queue can be defined as an ordered list which enables insert operations to be


performed at one end called REAR and delete operations to be performed at

i
another end called FRONT.

lin
2. Queue is referred to be as First In First Out list.

a
3. For example, people waiting in line for a rail ticket form a queue.

Sh
ith
W
a rn
Le
Applications of Queue

Due to the fact that queue performs actions on first in first out basis which is quite

i
lin
fair for the ordering of actions. There are various applications of queues discussed
as below.

a
Sh
• Queues are widely used as waiting lists for a single shared resource like

ith
printer, disk, CPU.
• Queues are used in asynchronous transfer of data (where data is not being
W
transferred at the same rate between two processes) for eg. pipes, file IO,
rn
sockets.
• Queues are used as buffers in most of the applications like MP3 media player,
a
Le

CD player, etc.
• Queue are used to maintain the play list in media players in order to add and
remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
What is the Queue?
1. A queue in the data structure can be considered similar to the queue in the
real-world.

i
lin
2. A queue is a data structure in which whatever comes first will go out first.

a
Sh
3. It follows the FIFO (First-In-First-Out) policy.

ith
4. In Queue, the insertion is done from one end known as the rear end or the
W
tail of the queue, whereas the deletion is done from another end known as
rn
the front end or the head of the queue.
a
Le
Le
arn
W
ith
Sh
a lin
i
Operations on Queue
There are two fundamental operations performed on a Queue:

1. Enqueue: The enqueue operation is used to insert the element at the rear

i
lin
end of the queue.

a
Sh
2. Dequeue: The dequeue operation performs the deletion from the front-end
of the queue. It also returns the element which has been removed from the

ith
front-end. It returns an integer value.

W
3. Peek: This is the third operation that returns the element, which is pointed
rn
by the front pointer in the queue but does not delete it.
a
Le

4. Queue overflow (isfull): When the Queue is completely full, then it shows
the overflow condition.

5. Queue underflow (isempty): When the Queue is empty, i.e., no elements


are in the Queue then it throws the underflow condition.
Implementation of Queue
There are two ways of implementing the Queue:

Sequential allocation: The sequential allocation in a Queue can be implemented

i
lin
using an array.

a
Sh
ith
W
Linked list allocation: The linked list allocation in a Queue can be implemented
rn
using a linked list.
a
Le
Types of Queue
There are four types of Queues
1. Linear Queue

i
lin
In Linear Queue, an insertion takes place from one end while the deletion occurs from
another end. The end at which the insertion takes place is known as the rear end, and the

a
end at which the deletion takes place is known as front end. It strictly follows the FIFO

Sh
rule.

ith
W
a rn
Le
• The major drawback of using a linear Queue is that insertion is
done only from the rear end.
• If the first three elements are deleted from the Queue, we cannot

i
lin
insert more elements even though the space is available in a

a
Linear Queue.

Sh
• In this case, the linear Queue shows the overflow condition as
the rear is pointing to the last element of the Queue.

ith
W
a rn
Le
2. Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue
except that the last element of the queue is connected to the first element. It is also known
as Ring Buffer as all the ends are connected to another end. The circular queue can be

i
represented as:

a lin
Sh
ith
W
a rn
Le

The drawback that occurs in a linear queue is overcome by using the circular queue. If the
empty space is available in a circular queue, the new element can be added in an empty
space by simply incrementing the value of rear.
3. Priority Queue
Each element has some priority associated with it. Based on the priority of the
element, the elements are arranged in a priority queue. If the elements occur with

i
lin
the same priority, then they are served according to the FIFO principle.
In priority Queue, the insertion takes place based on the arrival while the deletion

a
occurs based on the priority.

Sh
ith
4. Deque W
rn
Both the Linear Queue and Deque are different as the linear queue follows the FIFO
principle whereas, deque does not follow the FIFO principle. In Deque, the insertion
a
Le

and deletion can occur from both ends.


Array representation of Queue

There are two variables i.e. front and rear, that are implemented in the case of every queue.
Front and rear variables point to the position from where insertions and deletions are

i
lin
performed in a queue. Initially, the value of front and queue is -1 which represents an empty
queue.

a
Sh
ith
W
a rn
Le
Le
arn
W
ith
Sh
a lin
i
Le
arn
W
ith
Sh
a lin
i
Algorithm to insert any element in a queue
1. Check if the queue is already full by comparing rear to (max – 1). if so, then
return an overflow error.
2. If the item is to be inserted as the first element in the list, in that case set the
value of front and rear to 0 and insert the element at the rear end.
3. Otherwise keep increasing the value of rear and insert each element one by

i
lin
one having rear as the index.

a
Sh
ith
Algorithm
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step W
rn
[END OF IF]
a

Step 2: IF FRONT = -1 and REAR = -1


Le

SET FRONT = REAR = 0


ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT
Algorithm to delete an element from the queue

1. If, the value of front is -1 or value of front is greater than rear , write an
underflow message and exit.

2. Otherwise, keep increasing the value of front and return the item stored at the

i
lin
front end of the queue at each time.

a
Sh
ith
Algorithm

W
Step 1: IF FRONT = -1 or FRONT > REAR
rn
Write UNDERFLOW
ELSE
a

SET VAL = QUEUE[FRONT]


Le

SET FRONT = FRONT + 1


[END OF IF]
Step 2: EXIT
Drawback of array implementation
Although, the technique of creating a queue is easy, but there are some drawbacks of using
this technique to implement a queue.

i
lin
Memory wastage : The space of the array, which is used to store queue elements, can never
be reused to store the elements .

a
Sh
ith
W
a rn
Le
Deciding the array size

1. On of the most common problem with array implementation is the size of the
array which requires to be declared in advance.

i
lin
2. Due to the fact that, the queue can be extended at runtime depending upon the

a
problem, the extension in the array size is a time taking process and almost

Sh
impossible to be performed at runtime since a lot of reallocations take place.

ith
3. Due to this reason, we can declare the array large enough so that we can store
W
queue elements as enough as possible but the main problem with this
rn
declaration is that, most of the array slots (nearly half) can never be reused. It
will again lead to memory wastage.
a
Le

You might also like