0% found this document useful (0 votes)
17 views

Queue Notes

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)
17 views

Queue Notes

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/ 9

Queues

A queue is a linear collection of elements .It is a data structure in which whatever comes first
will go out first. It follows the FIFO (First-In-First-Out) policy. In Queue, the insertion is done
from one end known as the rear end or the tail of the queue, whereas the deletion is done from
another end known as the front end or the head of the queue.

The condition to insert an element when there is no space is called as gives an overflow
condition
The condition to delete an element when there is no space is called as gives an underflow
condition

Insertion in queue using arrays

Step 1: IF REAR = MAX - 1


Print “queue full”
Exit
[end if]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END IF]
Step 3: Set Q[REAR] = data
Step 4: EXIT
Deletion from queue

Step 1: IF FRONT = -1 or FRONT > REAR


Print “ UNDERFLOW”
Exit
[end if]
Step 2: SET data = Q[FRONT]
SET FRONT = FRONT + 1
[END IF]
Step 3: EXIT

Circular queue
In the 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 represented
as:

Insertion in circular queue

Step 1 if (front == 0 && rear == n - 1) then


Print “overflow queue full”
Exit
[end if]
Step 2 if (front == rear + 1) then
Print “overflow queue full”
Exit
[end if]
Step 3 if front == -1 then
front = 0
End if
If rear == n then
rear=1
Else
rear ++
Step 4 q[rear] = data
Step 5 exit

Deletion in circular queue

Step 1 if front==-1 then


Print "queue is empty"
Exit
End if
Step 2 if front==rear then
data=q[front]
Print “item deleted".data
front=-1;
rear=-1;
exit
End if
Step 3 data=q[front]
Print “item deleted".data
front++
Step 4 Exit
Linked Representation of Queue

Insertion in queue using link list


Deletion from queue using link list
Double Ended Queue

In a double ended queue, insertion and removal of elements can be performed from either from
the front or rear. Thus, it does not follow the FIFO (First In First Out) rule.z

There are two types of Queues, Input-restricted queue, and output-restricted queue.
1. Input-restricted queue: The input-restricted queue means that some restrictions are
applied to the insertion. In input-restricted queue, the insertion is applied to one end while
the deletion is applied from both the ends.
2. Output-restricted queue: The output-restricted queue means that some restrictions are
applied to the deletion operation. In an output-restricted queue, the deletion can be
applied only from one end, whereas the insertion is possible from both ends.

Priority queue

In a priority queue each element has some priority.The element with the highest priority would
come first in a priority queue. The priority of the elements in a priority queue will determine the
order in which elements are removed from the priority queue.
Following rules are applied to process the elements:
● An element with the higher priority will be deleted before the deletion of the lesser
priority.
● If two elements in a priority queue have the same priority, they will be arranged using the
FIFO principle.
Priority queues can be represented using following ways:
1. Priority queues using linklist
Each node is divided into 3 parts

The insertion takes place according to the priority of the queue while deletion takes place from
the front of link list
2. priority queues using multiple queues
It uses arrays as queues. In this a separate queue is maintained for all the elements with the same
priority which follows first in first out.They can also be used as circular queues. Each queue will
have a seperate front and rear.
To maintain such queues maximum no of elements with same priority and number of priority
levels have to be known in advance.

3. Priority queues using heaps

You might also like