You are on page 1of 16

QUEUE

QUEUE
 Queue is an abstract (LINEAR) data structure
 A queue is open at both its ends. One end is always used to insert data (enqueue)
and the other is used to remove data (dequeue).
 Element is inserted from one end called the REAR(also called tail), and the removal
of existing element takes place from the other end called as FRONT(also called
head).
 Queue follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
Basic features of Queue
1.Like stack, queue is also an ordered list of elements of similar data types.
2.Queue is a FIFO( First in First Out ) structure.
3.Once a new element is inserted into the Queue, all the elements inserted
before the new element in the queue must be removed, to remove the new
element.
4.peek( ) function is oftenly used to return the value of first element
without dequeuing it.
Basic Operations of Queue

• Enqueue: Add an element to the end of the queue


• Dequeue: Remove an element from the front of the queue
• IsEmpty: Check if the queue is empty
• IsFull: Check if the queue is full
• Peek: Get the value of the front of the queue without removing it
Applications of Queue Data Structure
• CPU scheduling, Disk Scheduling
• When data is transferred asynchronously between two processes.The
queue is used for synchronization. eg: IO Buffers, pipes, file IO, etc
• Handling of interrupts in real-time systems.
• Call Center phone systems use Queues to hold people calling them in
an order
Algorithm for ENQUEUE operation

• Step 1 − Check if the queue is full.


• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
• Step 4 − Add data element to the queue location, where the rear is
pointing.
• Step 5 − return success.
Case 2: F R N
Let us the queue A B c    

Front F=1 R=3 N=5 ITEM=D


Step 1: FRONT=1 & REAR=N
FRONT=1  1=1 true &
REAR=N  3=5false OR
FRONT=REAR+11=4False
Case 1: Step 2: FRONT=NULL FRONT=01=0false
F RN
A B c D e
Else if

F=1 R=5 N=5 REAR=N3=5false

Step 1: FRONT=1 and REAR=N Else

F=1 1=1 true & Set REAR=REAR+1REAR=3+1=4REAR=4

REAR = N 5=5true OR Step 3: QUEUE[REAR]= ITEM


QUEUE[4]=D
FRONT=REAR+1FRONT=6
A B c  D  
Print Overflow F R N
  Step 4: Exit
Dequeue Operation
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is
pointing.
• Step 4 − Increment front pointer to point to the next available data
element.
• Step 5 − Return success.
Case 2:
FRONT=-1 REAR=-1 N=5
Step 1: FRONT=-1-1=-1true
Print “UNDERFLOW” & Return.

Case 3:
F=1 N=5 R=4
F R N
Case 1: A B c d  

FR N Step 1: FRONT=-1 1=-1false


A        
Step 2: Element=Queue[FRONT]
FRONT=1 REAR=1 N=5
Element= Queue[1]Element=A
Step 1: FRONT=-11=-1False
A deleted
Step 2: Element=Queue[FRONT]
F R N
Element=Queue[1] Element=A   B c d  

ADeleted Step 3: FRONT=REAR 1=4false


Step 3: FRONT=REAR1=1true FRONT=FRONT+1FRONT=1+1=2
FRONT=REAR=-1 FRONT=2
       
  B c d  
Step 4: Return F R N
CIRCULAR QUEUE

• Circular Queue is a linear data structure in which the operations are


performed based on FIFO (First In First Out) principle and the last
position is connected back to the first position to make a circle. It is
also called ‘Ring Buffer’. 
• In a normal Queue, we can insert elements until queue becomes full.
But once queue becomes full, we can not insert the next element
even if there is a space in front of queue.
Operations on Circular Queue:
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
• enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the
new element is always inserted at Rear position. 
• Steps: Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
• If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it
is true then set rear=0 and insert element.
• deQueue() This function is used to delete an element from the circular queue. In a circular queue, the
element is always deleted from front position. 
• Steps: Check whether queue is Empty means check (front==-1).
• If it is empty then display Queue is empty. If queue is not empty then step 3
• Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is true then set
front=0 and return the element.
Basic features of Circular Queue
1.In case of a circular queue, head pointer will always point to the front of the queue, and tail pointer will
always point to the end of the queue.
2.Initially, the head and the tail pointers will be pointing to the same location, this would mean that the queue is
empty.

1. New data is always added to the location pointed by the tail pointer, and once the data is added, tail
pointer is incremented to point to the next available location.
In a circular queue, data is not actually removed from the queue. Only the head pointer is incremented by one
position when dequeue is executed. As the queue data is only the data between head and tail, hence the data
left outside is not a part of the queue anymore, hence removed.

•Also, the head and the tail pointers can cross each other. In other words, head
pointer can be greater than the tail. Sounds odd? This will happen when we
dequeue the queue a couple of times and the tail pointer gets reinitialised upon
reaching the end of the queue.

•The head and the tail pointer will get reinitialised to 0 every time they reach the end of the queue.
Applications of Circular Queue

• CPU scheduling
• Memory management
• Traffic Management

You might also like