#include<stdio.h>
void display(int[],int*,int*,int);
void cqinsert(int q[],int *front,int * rear,int n,int size)
{
if((*rear >= size-1 && *front==0) || *front==*rear+1)
{
printf("\nQueue Overflow...");
return;
}
else
{
if(*rear == size-1)
*rear=0;
else
(*rear)++;
q[*rear]=n;
if(*front==-1)
*front=0;
display(q,front,rear,size);
}
}
void display(int q[],int *front,int *rear,int size)
{
int i;
if(*front == -1)
{
printf("\nQueue Underflow..");
return;
}
else
{
printf("\n Queue :");
printf("\n");
for(i=*front;i!=*rear;i=(i+1)%size)
{
printf("| %d ",q[i]);
}
printf("| %d ",q[i]);
}
}
int cqdelete(int q[],int *front,int *rear,int size)
{
int temp;
if(*front==-1)
{
printf("\nQueue Underflow..");
return 0;
}
else
{
temp=q[*front];
if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
if(*front >= size-1)
*front=0;
else
(*front)++;
}
return temp;
}
}
void main()
{
int q[5],front=-1,rear=-1,n,opt;
int size=5;
clrscr();
again:
printf("\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter element : ");
scanf("%d",&n);
cqinsert(q,&front,&rear,n,size);
goto again;
break;
case 2: n=cqdelete(q,&front,&rear,size);
if(n!=0)
printf("\nDeleted Element : %d",n);
goto again;
break;
case 3: display(q,&front,&rear,size);
goto again;
break;
case 4: goto exit;
break;
default: printf("\nInvalid option...");
goto again;
break;
}
exit:
}
void display(int[],int*,int*,int);
void cqinsert(int q[],int *front,int * rear,int n,int size)
{
if((*rear >= size-1 && *front==0) || *front==*rear+1)
{
printf("\nQueue Overflow...");
return;
}
else
{
if(*rear == size-1)
*rear=0;
else
(*rear)++;
q[*rear]=n;
if(*front==-1)
*front=0;
display(q,front,rear,size);
}
}
void display(int q[],int *front,int *rear,int size)
{
int i;
if(*front == -1)
{
printf("\nQueue Underflow..");
return;
}
else
{
printf("\n Queue :");
printf("\n");
for(i=*front;i!=*rear;i=(i+1)%size)
{
printf("| %d ",q[i]);
}
printf("| %d ",q[i]);
}
}
int cqdelete(int q[],int *front,int *rear,int size)
{
int temp;
if(*front==-1)
{
printf("\nQueue Underflow..");
return 0;
}
else
{
temp=q[*front];
if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
if(*front >= size-1)
*front=0;
else
(*front)++;
}
return temp;
}
}
void main()
{
int q[5],front=-1,rear=-1,n,opt;
int size=5;
clrscr();
again:
printf("\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter element : ");
scanf("%d",&n);
cqinsert(q,&front,&rear,n,size);
goto again;
break;
case 2: n=cqdelete(q,&front,&rear,size);
if(n!=0)
printf("\nDeleted Element : %d",n);
goto again;
break;
case 3: display(q,&front,&rear,size);
goto again;
break;
case 4: goto exit;
break;
default: printf("\nInvalid option...");
goto again;
break;
}
exit:
}
No comments:
Post a Comment