#include<stdio.h>
void display(int q[],int *front,int *rear)
{
int i;
if(*front == -1)
{
printf("\nQueue Underflow...\n");
return;
}
else
{ printf("\n");
for(i=*front;i<=*rear;i++)
{
printf("| %d ",q[i]);
}
printf("|");
}
}
void insertrear(int q[],int *front,int * rear,int n,int size)
{ int i;
if(*rear >= size-1)
{
printf("\nQueue Overflow...\n");
return;
}
else
{
(*rear)++;
q[*rear]=n;
if(*front == -1)
*front=0;
printf("\n");
for(i=*front;i<=*rear;i++)
{
printf("| %d ",q[i]);
}
printf("|");
}
}
int deletefront(int q[],int *front,int *rear)
{
int x;
if(*front == -1)
{
printf("\nQueue Underflow ...\n");
return 0;
}
else
{
x=q[*front];
if(*front == *rear)
*front=*rear=-1;
else
(*front)++;
return x;
}
}
void insertfront(int q[],int *front,int *rear,int n)
{
if(*front == -1)
{
printf("\nQueue is Empty...\n");
return;
}
if(*front == 0)
{
printf("\nQueue Overflow...\n");
return;
}
(*front)--;
q[*front]=n;
display(q,front,rear);
}
int deleterear(int q[],int *front,int *rear)
{
int temp;
if(*rear == -1)
{
printf("\nQueue Underflow...\n");
return 0;
}
temp=q[*rear];
if(*front == *rear)
*front=*rear=-1;
else
(*rear)--;
return temp;
}
void main()
{
int q[5],front=-1,rear=-1,opt,n;
int size=5;
clrscr();
again:
printf("\n1. InsertAtRear\n2. InsertAtFront\n3. RemoveAtRear\n4. RemoveAtFront\n5. Display\n6. Exit");
printf("\n>> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter element At Rear: ");
scanf("%d",&n);
insertrear(q,&front,&rear,n,size);
goto again;
case 2:printf("\nEnter element At Front: ");
scanf("%d",&n);
insertfront(q,&front,&rear,n);
goto again;
case 3:n=deleterear(q,&front,&rear);
if(n!=0)
printf("\nDeleted Element : %d",n);
printf("\n");
goto again;
case 4: n=deletefront(q,&front,&rear);
if(n!=0)
printf("\nDeleted Element : %d",n);
goto again;
case 5: display(q,&front,&rear);
goto again;
case 6: goto exit;
default: printf("\nInvalid option...");
goto again;
}
exit:
}
void display(int q[],int *front,int *rear)
{
int i;
if(*front == -1)
{
printf("\nQueue Underflow...\n");
return;
}
else
{ printf("\n");
for(i=*front;i<=*rear;i++)
{
printf("| %d ",q[i]);
}
printf("|");
}
}
void insertrear(int q[],int *front,int * rear,int n,int size)
{ int i;
if(*rear >= size-1)
{
printf("\nQueue Overflow...\n");
return;
}
else
{
(*rear)++;
q[*rear]=n;
if(*front == -1)
*front=0;
printf("\n");
for(i=*front;i<=*rear;i++)
{
printf("| %d ",q[i]);
}
printf("|");
}
}
int deletefront(int q[],int *front,int *rear)
{
int x;
if(*front == -1)
{
printf("\nQueue Underflow ...\n");
return 0;
}
else
{
x=q[*front];
if(*front == *rear)
*front=*rear=-1;
else
(*front)++;
return x;
}
}
void insertfront(int q[],int *front,int *rear,int n)
{
if(*front == -1)
{
printf("\nQueue is Empty...\n");
return;
}
if(*front == 0)
{
printf("\nQueue Overflow...\n");
return;
}
(*front)--;
q[*front]=n;
display(q,front,rear);
}
int deleterear(int q[],int *front,int *rear)
{
int temp;
if(*rear == -1)
{
printf("\nQueue Underflow...\n");
return 0;
}
temp=q[*rear];
if(*front == *rear)
*front=*rear=-1;
else
(*rear)--;
return temp;
}
void main()
{
int q[5],front=-1,rear=-1,opt,n;
int size=5;
clrscr();
again:
printf("\n1. InsertAtRear\n2. InsertAtFront\n3. RemoveAtRear\n4. RemoveAtFront\n5. Display\n6. Exit");
printf("\n>> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter element At Rear: ");
scanf("%d",&n);
insertrear(q,&front,&rear,n,size);
goto again;
case 2:printf("\nEnter element At Front: ");
scanf("%d",&n);
insertfront(q,&front,&rear,n);
goto again;
case 3:n=deleterear(q,&front,&rear);
if(n!=0)
printf("\nDeleted Element : %d",n);
printf("\n");
goto again;
case 4: n=deletefront(q,&front,&rear);
if(n!=0)
printf("\nDeleted Element : %d",n);
goto again;
case 5: display(q,&front,&rear);
goto again;
case 6: goto exit;
default: printf("\nInvalid option...");
goto again;
}
exit:
}
No comments:
Post a Comment