Queue (First in First Out) Fifo
Queue (First in First Out) Fifo
Queue (First in First Out) Fifo
FIFO
Queue
A queue is logically a first in first out (FIFO) type of list. Queue is a non-primitive linear data structure. It is an
homogenous collection of elements in which new elements are added at one end called the REAR end and the
existing elements are deleted from other end called FRONT end.
Alternatively
Queue is a linear data structure in which the insertion and deletion operations are performed at two different
ends. In a queue data structure, adding and removing elements are performed at two different positions. The
insertion is performed at one end and deletion is performed at another end.
Initialize
FRONT= -1
REAR= -1
10 20 30 40 50
Queue ADT
Bool Isempty ()
Bool Isfull ()
void EnQueue ()
Type DeQueue ()
Void display()
Insertion [ Algorithm for insertion]
This algorithm inserts the item at the REAR of queue[maxsize]
Step 1 : If Queue is Full print Queue overflow and go to step 5 else go to step 2.
Step 2 : Read item
Step 3 : If FRONT= -1, then set FRONT: = 0 and REAR := 0
else
REAR: = REAR +1
End of if
Step 4 : Set Queue [ REAR ] := item
Step 5 : Exit
DeQueue
• This algorithm deletes the item at the FRONT of Queue[Maxsize]
Step 1: If Queue is empty then print Queue underflow and go to step 5 else go to step 2
Step 2: Set item:=Queue[FRONT]
Step 3:If FRONT=REAR
Set FRONT:=-1 and REAR:=-1
else,
FRONT:=FRONT+1
end if
Step 4:Print the deleted Item.
Step 5: Exit
• Bool IsEmpty() Void Display()
• If FRONT=-1 Step1: If Queue isEmpty() is true Display Queue is Empty and exit
else go to step 2
Return true Step 2: Set COUNTER:=FRONT
Else Return false Step 3: Repeat till COUNTER<=REAR
End if a. Print Queue[COUNTER]
b. Increment COUNTER=COUNTER+1
Bool IsFull()
Step 4:EXIT
If REAR=maxsize-1
Return true
Else
return false
End if
Circular Queue
Step 3 : Exit
Cqdelete ( Queue [ maxsize ],item)
Step 1 : If (FRONT=-1)
Write Queue underflow and exit.
Else Item:= Queue [ FRONT ]
if ( FRONT = REAR)
Set FRONT: = -1
REAR = -1
Else : FRONT: = ( FRONT +1)% maxsize
[ End if Structure ]
Step 2 : Exit
Display
• Step1: Set COUNTER:=FRONT
• Step2:If(FRONT=-1) Display Queue is Empty and Exit
• Step3:Repeat Step 4 to Step6 While(COUNTER≠REAR)
• Set4 Item:=Queue[COUNTER]
• Step5:Display Item
• Step6:Set COUNTER :=(COUNTER+1)%maxsize
• END OF Step3 LOOP
• Step7:Display Item(Item=Queue[COUNTER]
• Step8:EXIT
Priority Queue
• Priority Queue is an extension of queue with following properties.
• Every item has a priority associated with it.
• An element with high priority is dequeued before an element with
low priority.
• If two elements have the same priority, they are served according to
their order in the queue.
Types of Priority Queue
• 1. Ascending priority queue
A collection of an item into which item can be inserted arbitrary and
from which the smallest item is removed is called ascending priority
queue.
• 2. Descending priority queue
• A collection of an item into which item can be inserted arbitrary and
from which the largest item is removed is called descending priority
queue.
Applications of Priority Queue:
• 1) CPU Scheduling
• 2) Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s
Minimum Spanning Tree, etc
• 3) All queue applications where priority is involved.
• Input restricted Queue: In this type of Queue, the input can be taken
from one side only(rear) and deletion of element can be done from
both side(front and rear). This kind of Queue does not follow
FIFO(first in first out).
This queue is used in the cases where the consumption of the data needs to be in FIFO order but and if
there is a need to remove the recently inserted data for some reasons and one such case can be
irrelevant data, performance issue, etc.
Output restricted Queue: In this type of Queue, the input can be taken from both sides(rear and front) and
the deletion of the element can be done from only one side(front).
This queue is used in the case where the inputs have some priority order to be executed and the input can be
placed even in the first place so that it is executed first.
Double Ended Queue is also a Queue data structure in which the insertion and deletion operations are performed at
both the ends (front and rear). That means, we can insert at both front and rear positions and can delete from both front
and rear positions.
Since Deque supports both stack and queue operations, it can be used as both.
Applications of Queue Data Structure
• Queue is used when process 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.
• 1) When a resource is shared among multiple consumers. Examples include CPU Scheduling, Disk
Scheduling.
2) 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.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
Thank you