Showing posts with label QUEUE IN C. Show all posts
Showing posts with label QUEUE IN C. Show all posts

Thursday, May 13, 2010

QUEUE AS A LINKED LIST

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"dos.h"
#include"alloc.h"
void addq(int);
void delq();
void disq();
struct node
{
int d;
struct node *next;
};
struct node *front,*rear,*temp;

void main()
{
int ch,ele;
front=rear=temp=NULL;
clrscr();
do
{
printf("Enter\n1.Insert\n2.Remove\n3.Display\n4.Exit\n\nEnter choice here:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter element here:");
scanf("%d",&ele);
addq(ele);
break;
case 2:
delq();
break;
case 3:
disq();
break;
case 4:
exit(0);
default:
printf("\007\007Wrong Choice");
sleep(5);
}
}while(ch!=4);
getch();
}

void addq(int no)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->d=no;
temp->next=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=rear->next;
}
}

void delq()
{
if(front==NULL)
{
printf("\007 Q empty");
}
else
{
printf("\n element removed is %d",front->d);
front=front->next;
}
}
void disq()
{
if(front==NULL)
{
printf("\007\007 Q empty");
}
else
{
temp=front;
while(temp!=NULL)
{
printf("%d, ",temp->d);
temp=temp->next;
}
}
}