#include “stdio.h”
#include “conio.h”
#define NULL 0
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*newnode,*temp;
void main()
{
int ch;
clrscr();
do
{
printf("\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT");
printf("\n Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
}while(ch!=4);
getch();
}
enqueue()
{
newnode=(struct node*)malloc(sizeof (struct node));
printf("\n Enter The Data :");
scanf("%d",&newnode->data);
newnode->link=NULL;
printf("\n The inserted data is:%d",newnode->data);
if(front==NULL)
front=newnode;
else
rear->link=newnode;
rear=newnode;
}
dequeue()
{
temp=front;
if(front==NULL)
printf("\n Queue is empty");
else
{
printf("\n The deleted data is:%d",temp->data);
front=front->link;
}
free(temp);
}
display()
{
temp=front;
if(front==NULL)
printf("\n Queue is empty");
else
{
printf("\n Queue Values are:");
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
}
Output will be
Enter the queue size: 3
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 1
Enter The Data : 5
The inserted data is: 5
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 1
Enter The Data : 10
The inserted data is: 10
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 3
Queue Values are:
5
10
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 2
The deleted data is: 5
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 3
Queue Values are:
10
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 4
Google Ads
Monday, May 25, 2009
Queue Operation - Using Linked List
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment