Wednesday, 26 September 2012

Queue using Linked list in C program

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node*link;
};
struct node*cur,*first,*last;
void create();
void insert();
void del();
void display();
void create()
{
printf("\n enter the first element...");
cur=(struct node*)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void insert()
{
printf("\n enter the nxt element...");
cur=(struct node*)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
last->link=cur;
last=cur;
}
void del()
{
if(first==NULL)
{
printf("\n Queue is empty");
}
else
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\n deleted element is %d \n",cur->data);
free(cur);
}
}
void display()
{
cur=first;
printf("\n");
while(cur!=NULL)
{
printf("\n %d",cur->data);
cur=cur->link;
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.create\n2.insert\n3.delete\n4.display\n5.exit\n");
printf("\n enter your choice....");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:insert();
break;
case 3:del();
break;
case 4:display();
break;
case 5:exit(0);
}
}
}

No comments:

Post a Comment

There was an error in this gadget