#include<stdio.h>
#include<conio.h>
#define n 3
int front=-1,rear=-1;
int queue[n],item;
int isempty();
main()
{
void cqinsert();
void cqdelete();
void cqdisplay();
int choice;
clrscr();
while(1)
{
printf("\n circular queue simulator");
printf("\n 1.insert");
printf("\n 2.display");
printf("\n 3.delete");
printf("\n 4.exit ");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:cqinsert();
break;
case 2:cqdisplay();
break;
case 3:cqdelete();
break;
case 4:exit();
break;
default:printf("\n invalid entry ");
}
}
}
void cqinsert()
{
if(cqfull())
{
printf("\n queue is full - overflow");
return;
}
else
{
printf("\n enter the element to be added:");
scanf("%d",&item);
if(front==-1)
front=rear=0;
else
rear=(rear+1)%n;
queue[rear]=item;
}
return;
}
void cqdisplay()
{
int i;
if(cqempty())
{
printf ("queue is empty");
return;
}
printf("\n queue elements\n");
if(front<=rear)
{
for(i=front;i<=n-1;i++)
printf("\n%d",queue[i]);
}
if(front>rear)
{
for(i=front;i<=n-1;i++)
printf("\n%d",queue[i]);
for(i=0;i<=rear;i++)
printf("%n%d",queue[i]);
}
}
void cqdelete()
{
int item;
if(cqempty())
{
printf("\n underflow");
return;
}
else
{
item=queue[front];
printf("\n deleted elements is%d",item);
queue[front]=0;
if(front==rear)
{
printf("\n queue has a single element");
front=rear=-1;
}
else
front=(front+1)%n;
}
return;
}
int cqempty()
{
if((front==-1)&&(rear==-1))
return 1;
else
return 0;
}
int cqfull()
{
if((front==1)*(rear+1)%n)
return 1;
else
return 0;
}
#include<conio.h>
#define n 3
int front=-1,rear=-1;
int queue[n],item;
int isempty();
main()
{
void cqinsert();
void cqdelete();
void cqdisplay();
int choice;
clrscr();
while(1)
{
printf("\n circular queue simulator");
printf("\n 1.insert");
printf("\n 2.display");
printf("\n 3.delete");
printf("\n 4.exit ");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:cqinsert();
break;
case 2:cqdisplay();
break;
case 3:cqdelete();
break;
case 4:exit();
break;
default:printf("\n invalid entry ");
}
}
}
void cqinsert()
{
if(cqfull())
{
printf("\n queue is full - overflow");
return;
}
else
{
printf("\n enter the element to be added:");
scanf("%d",&item);
if(front==-1)
front=rear=0;
else
rear=(rear+1)%n;
queue[rear]=item;
}
return;
}
void cqdisplay()
{
int i;
if(cqempty())
{
printf ("queue is empty");
return;
}
printf("\n queue elements\n");
if(front<=rear)
{
for(i=front;i<=n-1;i++)
printf("\n%d",queue[i]);
}
if(front>rear)
{
for(i=front;i<=n-1;i++)
printf("\n%d",queue[i]);
for(i=0;i<=rear;i++)
printf("%n%d",queue[i]);
}
}
void cqdelete()
{
int item;
if(cqempty())
{
printf("\n underflow");
return;
}
else
{
item=queue[front];
printf("\n deleted elements is%d",item);
queue[front]=0;
if(front==rear)
{
printf("\n queue has a single element");
front=rear=-1;
}
else
front=(front+1)%n;
}
return;
}
int cqempty()
{
if((front==-1)&&(rear==-1))
return 1;
else
return 0;
}
int cqfull()
{
if((front==1)*(rear+1)%n)
return 1;
else
return 0;
}
No comments:
Post a Comment