Create circular Queue in data structure by R4R Team

program-

#include< sdtio.h>
#include< conio.h>
#include< alloc.h>
#define MAX 5

int circular_queue[MAX];
int front=-,rear=-1;

int insert()
{
int item;
if(front==0 && rear==MAX-1) || (front==rear+1)
{
printf("Queue is overflow\n");
return 0;
}
if(front==-1)
{
front=0;
rear=0;
}
else
{
if(rear==MAX-1)
rear=0;
else
rear=rear+1
}
printf("Enter the element for insertionn");
scanf("%d",&item);
circular_queue[rear]=item;
}

int display()
{
int f=first,r=rear;
if(f==-1)
{
printf("Queue is Empty\n");
return 0;
}
printf("Queue is :\n");
if(f< =r)
while(f< =r)
{
printf("%d ",circular_queue[f]);
f=f+1;
}
else
while(f< =MAX-1)
{
printf("%d ",circular_queue[f]);
f=f+1;
}
f=0;
while(f< =r)
{
printf("%d ",circular_queue[f]);
f=f+1;
}
}

void main()
{
int i;
clrscr();
display();
for(i=0;i< 5;i++)
{
insert();
}
display();
getch();
}


output-

Queue is Empty
Enter the element for insertion
3
Enter the element for insertion
4
Enter the element for insertion
1
Enter the element for insertion
5
Enter the element for insertion
9
Queue is :
3 4 1 5 9

-In this program,
- insert(): is used to insert the data in circular queue
- display() : is used to traverse the circular queue.




Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!