#include<stdio.h>
#define maxi 5
struct circularqueue // Structure of Circular Queue...
{
int a[maxi];
int f,r;
};
void insert(struct circularqueue *p,int x);
int delete(struct circularqueue *p);
void display(struct circularqueue q);
int flag; // Temporary variable to handle the error condition in delete function...
void main()
{
struct circularqueue c;
c.f=c.r=-1;
printf("Empty Queue is Created..");
int ch;
int temp=0; // Temporary variable to exit the menu driven program...
printf("\nChoice : 1) Insertion 2) Deletion 3) Display Queue 4) Stop");
while(1){
printf("\nEnter your choice..");
scanf("%d",&ch);
switch(ch){
case 1:
{
int x;
printf("\nEnter the element to be inserted..");
scanf("%d",&x);
insert(&c,x);
break;
}
case 2:
{
flag=0;
int t=delete(&c);
if(flag==0)
printf("\nThe Element which is deleted is..%d",t);
break;
}
case 3:{ display(c); break;}
case 4:{ temp=1; break; }
default: { printf("\nPlease enter a valid choice.."); }
}
if(temp==1)
break;
}
}
void insert(struct circularqueue *p,int x)
{
if((p->f==0&&p->r==maxi-1)||(p->r==p->f-1)){ // Corner case to handle if the queue is full...
printf("\nQueue Overflow..");
return;
}
else if(p->f==-1&&p->r==-1){ // Corner case to handle if the queue is empty...
p->f=p->r=0;
p->a[p->f]=x;
}
else{
p->r=(p->r+1)%maxi;
p->a[p->r]=x;
}
}
int delete(struct circularqueue *p)
{
int x;
if(p->f==-1&&p->r==-1){ // Corner case to handle if the queue is empty...
printf("\nCircular Queue is Empty..");
flag=1;
}
else if(p->f==p->r){ // Corner case to handle if the queue contains a single element...
x=p->a[p->f];
p->f=p->r=-1;
}
else{
x=p->a[p->f];
p->f=(p->f+1)%maxi;
}
return x;
}
void display(struct circularqueue q)
{
if(q.f==-1&&q.r==-1){ // Corner case to handle if the queue is empty...
printf("\nThe Queue is Empty..");
return;
}
printf("\nThe Queue is..");
while(q.f!=-1){ // Deleteting all the nodes of the local circular queue and displaying all the elements...
printf(" %d",delete(&q));
}
}
#define maxi 5
struct circularqueue // Structure of Circular Queue...
{
int a[maxi];
int f,r;
};
void insert(struct circularqueue *p,int x);
int delete(struct circularqueue *p);
void display(struct circularqueue q);
int flag; // Temporary variable to handle the error condition in delete function...
void main()
{
struct circularqueue c;
c.f=c.r=-1;
printf("Empty Queue is Created..");
int ch;
int temp=0; // Temporary variable to exit the menu driven program...
printf("\nChoice : 1) Insertion 2) Deletion 3) Display Queue 4) Stop");
while(1){
printf("\nEnter your choice..");
scanf("%d",&ch);
switch(ch){
case 1:
{
int x;
printf("\nEnter the element to be inserted..");
scanf("%d",&x);
insert(&c,x);
break;
}
case 2:
{
flag=0;
int t=delete(&c);
if(flag==0)
printf("\nThe Element which is deleted is..%d",t);
break;
}
case 3:{ display(c); break;}
case 4:{ temp=1; break; }
default: { printf("\nPlease enter a valid choice.."); }
}
if(temp==1)
break;
}
}
void insert(struct circularqueue *p,int x)
{
if((p->f==0&&p->r==maxi-1)||(p->r==p->f-1)){ // Corner case to handle if the queue is full...
printf("\nQueue Overflow..");
return;
}
else if(p->f==-1&&p->r==-1){ // Corner case to handle if the queue is empty...
p->f=p->r=0;
p->a[p->f]=x;
}
else{
p->r=(p->r+1)%maxi;
p->a[p->r]=x;
}
}
int delete(struct circularqueue *p)
{
int x;
if(p->f==-1&&p->r==-1){ // Corner case to handle if the queue is empty...
printf("\nCircular Queue is Empty..");
flag=1;
}
else if(p->f==p->r){ // Corner case to handle if the queue contains a single element...
x=p->a[p->f];
p->f=p->r=-1;
}
else{
x=p->a[p->f];
p->f=(p->f+1)%maxi;
}
return x;
}
void display(struct circularqueue q)
{
if(q.f==-1&&q.r==-1){ // Corner case to handle if the queue is empty...
printf("\nThe Queue is Empty..");
return;
}
printf("\nThe Queue is..");
while(q.f!=-1){ // Deleteting all the nodes of the local circular queue and displaying all the elements...
printf(" %d",delete(&q));
}
}
No comments:
Post a Comment