Queue using linked list.

Leave a Comment
#include<sdtio.h>
#include<stdlib.h>
#include<conio.h>

struct node{
int n;
struct node *next;
};
struct node *front=NULL,*rear=NULL;



void addq()
{
struct node *k;
k=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the number:");
scanf("%d",&k->n);
k->next=NULL;
if(front==NULL&&rear==NULL)
{
front=k;
rear=k;
}
else
{
rear->next=k;
rear=k;
}
}


void delq()
{

if(front==NULL&&rear==NULL)
printf("\nThe queue is empty!!");
else
front=front->next;

}
void print()
{
struct node *t;
t=front;
while(t!=NULL)
{
printf("%d\t",t->n);
t=t->next;
}
}

void isempty()
{
if(front==NULL&&rear==NULL)
printf("\nThe queue is empty!!");
}


int main()
{
int o;
while(o!=0)
{
printf("\nMENU");
printf("\n1 for add element");
printf("\n2 for delete element");
printf("\n3 for PRINT");
printf("\n4 for CHECKING EMPTY");
printf("\n0 for EXIT");
printf("\nEnter ur choice");
scanf("%d",&o);
switch(o)
{
case 1:addq();break;
case 2:delq();break;
case 3:print();break;
case 4:isempty();break;
case 0:exit(0);break;
default:printf("\nU have entered a wrong choice!!!");
}
}
getche();
}

0 comments: