Monday 23 February 2015

Queue Program in C


#include<stdio.h>
#include<conio.h>

struct Queue
{
int Data;
struct Queue *Next;
}*Front,*Rear;

main()
{
int choice;
Home:
clrscr();
printf("\n\t\t\t Queue Demo \n");
printf("\n\t\t\t1.. Push");
printf("\n\t\t\t2.. Pop");
printf("\n\t\t\t3.. Display");
printf("\n\t\t\t4.. Exit");
printf("\n\n\t\t Enter Your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
printf("\n Wrong Press");
}
goto Home;
}

Push()
{
struct Queue *Source=(struct Queue*)malloc(sizeof(struct Queue));

printf("\n Enter Data For Queue\n");
scanf("%d",&Source->Data);
Source->Next=NULL;

if(Front==NULL)
{
Front=(struct Queue*)malloc(sizeof(struct Queue));
Rear=(struct Queue*)malloc(sizeof(struct Queue));
Front=Rear=Source;
}
else
{
Rear->Next=Source;
Rear=Source;
}
}

Pop()
{
struct Queue *Source=(struct Queue*)malloc(sizeof(struct Queue));
Source=Front;
if(Source==NULL)
{
printf("\n Queue Is Empty");
}
else
{
printf("\n Poped Item is :%d",Source->Data);
Front=Front->Next;
free(Source);
}
getch();
}
Display()
{
struct Queue *Source=(struct Queue*)malloc(sizeof(struct Queue));
Source=Front;
while(Source!=NULL)
{
printf("\n Queue Item is :%d",Source->Data);
Source=Source->Next;
}
getch();
}

No comments:

Post a Comment