C language is always very useful to implements data structure. This program will be highly helpful for the students and professional to use queue using C language.
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
void push(int queue[],int *front,int *rear,int item)
{
if((*front==0 && *rear==MAX) || (*rear+1==*front))
{
printf("\nOver Flow.....");
return;
}
else if(*front==-1)
{
*front=0;
*rear=0;
}
else if(*rear==MAX)
{
*rear=0;
}
else
{
*rear=*rear+1;
}
queue[*rear]=item;
}
int pop(int queue[],int *front,int *rear)
{
int a;
if(*front==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=queue[*front];
if(*front==MAX)
{
*front=0;
}
else if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
{
*front=*front+1;
}
return a;
}
}
void display(int queue[],int front,int rear)
{
int i;
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
printf("\nPosition: %d\tVaue: %d",i,queue[i]);
}
}
else
{
for(i=0;i<=rear;i++)
{
printf("\nPosition: %d\tVaue: %d",i,queue[i]);
}
for(i=front;i<=MAX;i++)
{
printf("\nPosition: %d\tVaue: %d",i,queue[i]);
}
}
}
int menu()
{
int c;
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
return c;
}
void main()
{
int queue[10];
int front=-1,rear=-1;
int c,item;
while(1)
{
c=menu();
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(queue,&front,&rear,item);
break;
case 2:
item=pop(queue,&front,&rear);
if(item!=-1)
{
printf("%d has been deleted...",item);
}
break;
case 3:
display(queue,front,rear);
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
0 comments:
Post a Comment
You are most welcome for making comments here. I will consider your view and will reply you soon.