Queue

Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

Queue

Definition of Queue
⚫ Queue is an abstract data structure, somewhat similar
to Stacks. Unlike stacks, 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).
⚫ Queue follows First-In-First-Out methodology, i.e.,
the data item stored first will be accessed first.
Basic operations in Queue
Two basic operation:
⚫ enqueue() − add (store) an item to the queue.
⚫ dequeue() − remove (access) an item from the queue.

⚫ Functions are required to make the above-mentioned


queue operation efficient. These are −
⚫ peek() − Gets the element at the front of the queue
without removing it.
⚫ isfull() − Checks if the queue is full.
⚫ isempty() − Checks if the queue is empty.
peek()

⚫ This function helps to see the data at the front of the queue.
The algorithm of peek() function is as follows −
⚫ Algorithm
begin procedure peek
return queue[front]
end procedure
▪ Implementation of peek() function in C programming
language −
Example
int peek()
{
return queue[front];
}
isfull()

⚫ In Queue have to check the rear pointer to reach at


MAXSIZE to determine that the queue is full.
⚫ Algorithm:
Example
isempty()
If the value of front is less than MIN or 0, it tells that the queue is not yet
initialized, hence empty.
example
Enqueue
⚫ Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to
implement than that of stacks.
⚫ The following steps should be taken to enqueue (insert)
data into a queue −
⚫ 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.
Algorithm for enqueue operation
Example
Dequeue Operation

⚫ Accessing data from the queue is a process of two tasks −


access the data where front is pointing and remove the data
after access.
⚫ The following steps are taken to
perform 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.
Algorithm for dequeue operation
Example
Types of Queue
⚫ There are four different types of queues:
⚫ Simple Queue
⚫ Circular Queue
⚫ Priority Queue
⚫ Double Ended Queue
Simple Queue

⚫ In a simple queue, insertion takes place at the rear and


removal occurs at the front. It strictly follows the FIFO
(First in First out) rule.
Circular Queue

⚫ In a circular queue, the last element points to the first


element making a circular link.
Advantage
⚫ The main advantage of a circular queue over a simple
queue is better memory utilization.
⚫ If the last position is full and the first position is
empty, we can insert an element in the first position.
This action is not possible in a simple queue.
Priority Queue
⚫ A priority queue is a special type of queue in which
each element is associated with a priority and is served
according to its priority.
⚫ If elements with the same priority occur, they are
served according to their order in the queue.
⚫ Insertion occurs based on the arrival of the values and
removal occurs based on priority.
Deque (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.
Types of Deque

⚫ Input Restricted Deque


In this deque, input is restricted at a single end but
allows deletion at both the ends.
⚫ Output Restricted Deque
In this deque, output is restricted at a single end but
allows insertion at both the ends.
Implementation of Queue using array
⚫ 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 performed in a queue.
⚫ Initially, the value of front and queue is -1 which
represents an empty queue.
#include<stdio.h> switch(choice){
#include<conio.h> case 1: printf("Enter the
#define SIZE 10 value to be insert: ");
scanf("%d",&value);
void enQueue(int);
enQueue(value);
void deQueue();
void display(); break;
case 2: deQueue();
int queue[SIZE], front = -1, rear = -1; break;
case 3: display();
void main()
break;
{
int value, choice; case 4: exit(0);
clrscr(); default: printf("\nWrong
while(1){ selection!!! Try again!!!");
printf("\n\n***** MENU }
*****\n");
}
printf("1. Insertion\n2. Deletion\n3.
Display\n4. Exit"); }
printf("\nEnter your choice: ");
scanf("%d",&choice);
void enQueue(int value){
if(rear == SIZE-1) void display(){
printf("\nQueue is Full!!! Insertion is
not possible!!!"); if(rear == -1)
else{
if(front == -1)
printf("\nQueue is
front = 0; Empty!!!");
rear++;
queue[rear] = value; else{
printf("\nInsertion success!!!"); int i;
}
} printf("\nQueue
void deQueue(){
if(front == rear)
elements are:\n");
printf("\nQueue is Empty!!! Deletion
is not possible!!!");
for(i=front; i<=rear;
else{ i++)
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear) printf("%d\t",queue[i]);
front = rear = -1;
} }
}
}
Types of Queue-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.
⚫ Front: Get the front item(first item) from queue.
⚫ Rear: Get the last item from queue.
Operation on Circular Queue
steps of enQueue operation
⚫ First, we will check whether the Queue is full or not.
⚫ Initially the front and rear are set to -1. When we insert the first
element in a Queue, front and rear both are set to 0.
⚫ When we insert a new element, the rear gets incremented,
i.e., rear=rear+1.
⚫ Scenarios for inserting an element
⚫ There are two scenarios in which queue is not full:
⚫ If rear != max - 1, then rear will be incremented
to mod(maxsize) and the new value will be inserted at the rear end
of the queue.
⚫ If front != 0 and rear = max - 1, it means that queue is not full,
then set the value of rear to 0 and insert the new element there.
⚫ There are two cases in which the element cannot be inserted:
⚫ When front ==0 && rear = max-1, which means that front is at
the first position of the Queue and rear is at the last position of the
Queue.
enQueue Operation
⚫ 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.
⚫ 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.
steps of dequeue operation

⚫ First, check whether the Queue is empty or not. If the


queue is empty, we cannot perform the dequeue
operation.
⚫ When the element is deleted, the value of front gets
incremented by 1.
⚫ If there is only one element left which is to be deleted,
then the front and rear are reset to -1.
deQueue Operation
⚫ 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.
⚫ 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.
Example
enQueue operation
enQueue operation
deQueue operation
enQueue Operation
enQueue operation
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.

}
deQueue
int dequeue()
Operation
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
Displaying Elements of Queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
Time Complexity
⚫ Time complexity is the amount of time taken by an
algorithm to run, as a function of the length of the
input .
⚫ Time complexity of enQueue(), deQueue() operation is
O(1) as there is no loop in any of the operation.
Applications of Circular Queue

⚫ Memory management: The circular queue provides


memory management. a circular queue, the memory is
managed efficiently by placing the elements in a
location which is unused.
⚫ CPU Scheduling: The operating system also uses the
circular queue to insert the processes and then execute
them.
⚫ Traffic system: In a computer-control traffic system,
traffic light is one of the best examples of the circular
queue. Each light of traffic light gets ON one by one
after every interval of time. Like red light gets ON for
one minute then yellow light for one minute and then
green light. After green light, the red light gets ON.
Types of Queue- Priority queue
⚫ A priority queue is an abstract data type that behaves
similarly to the normal queue except that each element
has some priority, i.e., 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.
Priority queue
Characteristics of a Priority queue

⚫ Every element in a priority queue has some priority


associated with it.
⚫ 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.
Operations on Priority Queue
Implementation
Types of list
Types of list
Types of Priority Queue
⚫ Ascending order priority queue: In ascending order
priority queue, a lower priority number is given as a
higher priority in a priority.
⚫ For example, we take the numbers from 1 to 5
arranged in an ascending order like 1,2,3,4,5;
therefore, the smallest number, i.e., 1 is given as the
highest priority in a priority queue.
Types of Priority Queue
⚫ Descending order priority queue: In descending
order priority queue, a higher priority number is given
as a higher priority in a priority.
⚫ For example, we take the numbers from 1 to 5
arranged in descending order like 5, 4, 3, 2, 1;
therefore, the largest number, i.e., 5 is given as the
highest priority in a priority queue.
Inserting Element in queue
Inserting Element
Inserting small priority element
THANK YOU

You might also like