Sample program for Queue using linked list is;
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void insert();
void delet();
void display();
struct node *getnode(int);
struct node *f=NULL,*r=NULL;
void main()
{
int choice;
while(1)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n0. Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 0:
exit(0);
break;
default:
printf("\nWrong choice!!");
}
}
}
struct node *getnode(int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->link=NULL;
return(temp);
}
void insert()
{
struct node *temp;
int item;
printf("\nEnter New Node : ");
scanf("%d",&item);
temp=getnode(item);
if(f==NULL && r==NULL)
{
f=temp;
r=temp;
}
else
{
r->link=temp;
r=temp;
}
}
void display()
{
struct node *ptr;
for(ptr=f;ptr!=NULL;ptr=ptr->link)
printf("%d ",ptr->data);
}
void delet()
{
struct node *ptr;
if(f==NULL && r==NULL)
printf("\nQueue is empty\n");
else
{
ptr=f;
f=f->link;
ptr->link=NULL;
printf("\n%d is deleted..",ptr->data);
free(ptr);
}
}