0% found this document useful (0 votes)
161 views8 pages

Circular Queue Insertion and Deletion Algorithms

The document discusses circular queues, which are a data structure that allows addition of data at the end of the queue and removal of data from the beginning. Circular queues have a fixed size and follow FIFO principles. Algorithms for insertion and deletion in a circular queue are presented, including pseudocode and C code implementations. For insertion, if the queue is full, it results in an overflow, otherwise the item is added to the rear. For deletion, if the queue is empty, it results in an underflow, otherwise the front item is removed and the front pointer is incremented.

Uploaded by

Jesvin Bency
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)
161 views8 pages

Circular Queue Insertion and Deletion Algorithms

The document discusses circular queues, which are a data structure that allows addition of data at the end of the queue and removal of data from the beginning. Circular queues have a fixed size and follow FIFO principles. Algorithms for insertion and deletion in a circular queue are presented, including pseudocode and C code implementations. For insertion, if the queue is full, it results in an overflow, otherwise the item is added to the rear. For deletion, if the queue is empty, it results in an underflow, otherwise the front item is removed and the front pointer is incremented.

Uploaded by

Jesvin Bency
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

8/17/2016

CircularQueues

SortingAlgorithms
Algorithms
Tree
OnlineShopping

CircularQueue
Acircularqueueisanabstractdatatypethatcontainsacollectionofdatawhichallowsadditionofdataatthe
endofthequeueandremovalofdataatthebeginningofthequeue.Circularqueueshaveafixedsize.
CircularqueuefollowsFIFOprinciple.Queueitemsareaddedattherearendandtheitemsaredeletedatfront
endofthecircularqueue.

AlgorithmforInsertioninacircularqueue

InsertCircularQueue()

1.If(FRONT==1andREAR==N)or(FRONT==REAR+1)Then

2.
Print:Overflow

3.Else

4.
If(REAR==0)Then[CheckifQUEUEisempty]
http://scanftree.com/Data_Structure/circularqueue

2/10

8/17/2016

CircularQueues

(a)SetFRONT=1

(b)SetREAR=1

5.ElseIf(REAR==N)Then[IfREARreachesendifQUEUE]

6.

SetREAR=1

7.Else

8.

SetREAR=REAR+1[IncrementREARby1]

[EndofStep4If]

9.SetQUEUE[REAR]=ITEM

10.Print:ITEMinserted

[EndofStep1If]

11.Exit

Implementationofinsertionincircularqueue

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<ctype.h>

#include<stdlib.h>

#definesize8

intrear,front;

intno;

intq[size];

intrear=1;

intfront=1;

http://scanftree.com/Data_Structure/circularqueue

3/10

8/17/2016

CircularQueues

/*Functiontocreatequeue*/

voidInsert_queue()

charans;

printf("\nEnter'n'forbreak:");

ans=getch();

while(ans!='n')

printf("\nInputtheElement:");

scanf("%d",&no);

if((front==0)&&(rear==size1))

printf("\nQueueisOverflow");

return;

elseif(rear==front1)

printf("\nQueueisoverflow");

return;

elseif(front<0)/*InsertFirstElement*/

front=0;

rear=0;

http://scanftree.com/Data_Structure/circularqueue

4/10

8/17/2016

CircularQueues

q[rear]=no;

elseif(rear==size1)

rear=0;

q[rear]=no;

else

rear++;

if(rear==front)

printf("\nQueueisoverflow");

return;

else

q[rear]=no;

printf("\nEnter'n'forbreak:");

ans=getch();

voidDisplay_queue()

http://scanftree.com/Data_Structure/circularqueue

5/10

8/17/2016

CircularQueues

inti;

if(front<0)

printf("\nQueueisunderflow");

return;

printf("\nItemsare:\n");

if(rear>=front)

for(i=front;i<=rear;i++)

printf("\nq[%d]=%d",i,q[i]);

else

for(i=front;i<size;i++)

printf("\nl2q[%d]=%d",i,q[i]);

for(i=0;i<=rear;i++)

printf("\nl3q[%d]=%d",i,q[i]);

/*Functionmain*/

http://scanftree.com/Data_Structure/circularqueue

6/10

8/17/2016

CircularQueues

voidmain()

clrscr();

Insert_queue();

Display_queue();

getch();

AlgorithmforDeletioninacircularqueue

DeleteCircularQueue()

1.If(FRONT==0)Then[CheckforUnderflow]

2.
Print:Underflow

http://scanftree.com/Data_Structure/circularqueue

7/10

8/17/2016

CircularQueues

3.Else

4.
ITEM=QUEUE[FRONT]

5.

If(FRONT==REAR)Then[Ifonlyelementisleft]

(a)SetFRONT=0

(b)SetREAR=0

6.ElseIf(FRONT==N)Then[IfFRONTreachesendifQUEUE]

7.

SetFRONT=1

8.Else

9.

SetFRONT=FRONT+1[IncrementFRONTby1]

[EndofStep5If]

10.Print:ITEMdeleted

[EndofStep1If]

11.Exit

Cfuctionofdeletionincircularqueue

voidDelete_queue()

if(front<0)

printf("\nQueueisUnderflow");

return;

no=q[front];

q[front]=NULL;

printf("\nElementdeleted:",no);

if(front==rear)
http://scanftree.com/Data_Structure/circularqueue

8/10

8/17/2016

CircularQueues

front=1;

rear=1;

}
elseif(front==size1)
{

front=0;

}
else
{

front++;

Next

http://scanftree.com/Data_Structure/circularqueue

9/10

You might also like