Implement Queue using linked lists.
#include<stdio.h>
struct node
{
int data;
struct node *link;
}*header,*front,*rear,*ptr,*new;
void enqueue();
void dequeue();
void traverse();
void status();
int main ()
{
int ch;
clrscr();
header->link=NULL;
while(1)
{
printf("\n Enter Your Choice\n 1.Enqueue \n 2.Dequeue\n 3.Traverse\n 4.Status\n 5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:traverse();
break;
case 4:status();
break;
default:exit(0);
}
}
return 0;
}
void enqueue()
{
int item;
new=(struct node*)malloc(sizeof(struct node));
if (new==NULL)
{
printf("\n No Space");
exit(0);
}
else
{
printf("Enter The Element to insert:");
scanf("%d",&item);
if(front==NULL&&rear==NULL)
{
new->data=item;
header->link=new;
new->link=NULL;
front=new;
rear=new;
}
else
{
new->data=item;
rear->link=new;
new->link=NULL;
rear=new;
}
}
}
void dequeue()
{
if (front==NULL && rear==NULL)
{
printf("\n Queue is Empty");
exit(0);
}
else
{
if(front==rear)
{
ptr=front;
header->link=NULL;
printf("\n %d is deleted",front->data);
front=NULL;
rear=NULL;
free(ptr);
}
else
{
ptr=front;
header->link=front->link;
printf("%d is deleted",front->data);
front=header->link;
free(ptr);
}
}
}
void traverse()
{
if(front==NULL && rear==NULL)
printf("\n Queue is Empty");
else
{
ptr=header;
while(ptr->link!=NULL)
{
ptr=ptr->link;
printf("Data Parts of Queue is",ptr->data);
}
}
}
void status()
{
if (front==NULL && rear==NULL)
printf("\n Queue is Empty");
else
{
printf("\n Front element is %d",front->data);
printf("\n Rear element is %d",rear->data);
}
}
Comments
Post a Comment