queue link list
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
void main()
{
struct node *head=NULL,*ptr=NULL,*front=NULL,*rear=NULL,*temp=NULL,*ptr2;
int n;
clrscr();
printf("queue in link list\n");
while(1)
{
printf("press 1 for insert in queue\npress 2 for delete in queue\npress 3 for view the queue\npress 4 for exit the queue\n");
scanf("%d",&n);
switch(n)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
printf("enter the new data in node\n");
scanf("%d",&temp->data);
temp->link=NULL;
if(head==NULL)
{
head=temp;
front=head;
rear=temp;
}
else
{
rear=head;
while(rear->link!=NULL)
rear=rear->link;
rear->link=temp;
}
break;
case 2:
if(front==NULL)
{
printf("queue is empty\n");
}
else
{ printf("deleted element in queue is = %d\n",front->data);
ptr2=front;
front=front->link;
ptr2->link=NULL;
head=front;
free(ptr2);
}
break;
case 3 :
if(head==NULL)
printf("queue is empty\n");
else
{
ptr=front;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->link;
}}
break;
case 4:
exit(0);
break ;
default :
printf("enter the wrong value\n");
break;
}}
getch();
}
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
void main()
{
struct node *head=NULL,*ptr=NULL,*front=NULL,*rear=NULL,*temp=NULL,*ptr2;
int n;
clrscr();
printf("queue in link list\n");
while(1)
{
printf("press 1 for insert in queue\npress 2 for delete in queue\npress 3 for view the queue\npress 4 for exit the queue\n");
scanf("%d",&n);
switch(n)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
printf("enter the new data in node\n");
scanf("%d",&temp->data);
temp->link=NULL;
if(head==NULL)
{
head=temp;
front=head;
rear=temp;
}
else
{
rear=head;
while(rear->link!=NULL)
rear=rear->link;
rear->link=temp;
}
break;
case 2:
if(front==NULL)
{
printf("queue is empty\n");
}
else
{ printf("deleted element in queue is = %d\n",front->data);
ptr2=front;
front=front->link;
ptr2->link=NULL;
head=front;
free(ptr2);
}
break;
case 3 :
if(head==NULL)
printf("queue is empty\n");
else
{
ptr=front;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->link;
}}
break;
case 4:
exit(0);
break ;
default :
printf("enter the wrong value\n");
break;
}}
getch();
}