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

Queue Data Structure

Uploaded by

Fact Guru
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)
3 views

Queue Data Structure

Uploaded by

Fact Guru
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/ 33

QUEUE DATA

STRUCTURE
QUEUE DATA STRUCTURE
A Queue Data Structure is a fundamental concept in computer science used for
storing and managing data in a specific order. It follows the principle of “First in, First
out” (FIFO), where the first element added to the queue is the first one to be
removed.
BASIC OPERATIONS OF QUEUE
A queue is an object (an abstract data structure - ADT) that allows the
following operations:
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
WORKING OF QUEUE
Queue operations work as follows:

two pointers FRONT and REAR

FRONT track the first element of the queue

REAR track the last element of the queue

initially, set value of FRONT and REAR to -1


TYPES OF QUEUE
SIMPLE QUEUE OR LINEAR QUEUE
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 end at which the deletion takes place is
known as front end.
CIRCULAR QUEUE
A circular queue is the extended version of a
regular queue where the last element is connected
to the first element. Thus forming a circle-like
structure.
CIRCULAR QUEUE
The circular queue solves the major limitation of the normal queue. In a
normal queue, after a bit of insertion and deletion, there will be non-usable
empty space.
CIRCULAR QUEUE OPERATIONS
The circular queue work as follows:

two pointers FRONT and REAR

FRONT track the first element of the queue

REAR track the last elements of the queue

initially, set value of FRONT and REAR to -1


ENQUEUE OPERATION
check if the queue is full

for the first element, set value of FRONT to 0

circularly increase the REAR index by 1 (i.e. if the rear reaches the end,

next it would be at the start of the queue)

add the new element in the position pointed to by REAR


DEQUEUE OPERATION
check if the queue is empty

return the value pointed by FRONT

circularly increase the FRONT index by 1

for the last element, reset the values of FRONT and REAR to -1
CIRCULAR QUEUE OPERATION
CIRCULAR QUEUE OPERATION
CIRCULAR QUEUE OPERATION
CIRCULAR QUEUE OPERATION
CIRCULAR QUEUE OPERATION
CIRCULAR QUEUE OPERATION
void enque (int x){
if (front==-1 && rear==-1){
front=rear=0;
queue[rear]=x;
}
else if((rear+1)%N)==front){
queue is full
}
else{
rear=(rear+1)%N;
queue[rear]=x;
}
}
CIRCULAR QUEUE OPERATION
void enque (int x){
if (front==-1 && rear==-1){
front=rear=0;
queue[rear]=x;
}
else if((rear+1)%N)==front){
queue is full
}
else{
rear=(rear+1)%N;
queue[rear]=x;
}
}
CIRCULAR QUEUE OPERATION
void enque (int x){
if (front==-1 && rear==-1){
front=rear=0;
queue[rear]=x;
}
else if((rear+1)%N)==front){
queue is full
}
else{
rear=(rear+1)%N;
queue[rear]=x;
}
}
CIRCULAR QUEUE OPERATION
void enque (int x)
{
if (front==-1 && rear==-1)
{
queue is empty
}
else if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%N;
}
}
DOUBLE-ENDED QUEUE
The deque stands for Double Ended Queue. Deque is a linear data structure
where the insertion and deletion operations are performed from both ends.
Though the insertion and deletion in a deque can be performed on both
ends, it does not follow the FIFO rule.
DOUBLE-ENDED QUEUE-TYPES
There are two types of deque -
Input restricted queue
Output restricted queue

In input restricted queue, insertion operation can be performed at only


one end, while deletion can be performed from both ends.
DOUBLE-ENDED QUEUE-TYPES
In output restricted queue, deletion operation can be performed at only
one end, while insertion can be performed from both ends.
OPERATIONS PERFORMED ON DEQUE
Insertion at front
Insertion at rear
Deletion at front
Deletion at rear

Get the front item from the deque


Get the rear item from the deque
Check whether the deque is full or not
Checks whether the deque is empty or not
INSERTION AT FRONT
else if(f==0)
void insert_front(int x)
{
{
f=size-1;
if((f==0 && r==size-1) || (f==r+1))
deque[f]=x;
{
}
printf("Overflow");
else
}
{
else if((f==-1) && (r==-1))
f=f-1;
{
deque[f]=x;
f=r=0;
}
deque[f]=x;
}
}
INSERTION AT FRONT
INSERTION AT REAR
else if(r==size-1)
void insert_rear(int x)
{
{
r=0;
if((f==0 && r==size-1) || (f==r+1))
deque[r]=x;
{
}
printf("Overflow");
else
}
{
else if((f==-1) && (r==-1))
r++;
{
deque[r]=x;
r=0;
}
deque[r]=x;
}
}
INSERTION AT REAR
DELETION FROM FRONT
else if(f==(size-1))
void delete_front()
{
{
printf("\nThe deleted element is %d",
if((f==-1) && (r==-1))
deque[f]);
{
f=0;
printf("Deque is empty");
}
}
else
else if(f==r)
{
{
printf("\nThe deleted element is %d",
printf("deleted element is %d", deque[f]);
deque[f]);
f=-1;
f=f+1;
r=-1;
}
}
}
DELETION FROM FRONT
DELETION FROM REAR
else if(r==0)
void delete_rear()
{
{
printf("\nThe deleted element is %d",
if((f==-1) && (r==-1))
deque[r]);
{
r=size-1;
printf("Deque is empty");
}
}
else
else if(f==r)
{
{
printf("\nThe deleted element is %d",
printf("\nThe deleted element is %d", deque[r]);
deque[r]);
f=-1;
r=r-1;
r=-1;
}
}
}
DELETION FROM REAR

You might also like