Queue using Linked List
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
}*front=NULL,*rear=NULL;
void enqueue()
{
struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
int dequeue()
{
if (front==NULL)
{printf("Queue is empty...underflow");return -1;}
else
{
struct Node *temp;
temp=front;
int rv=front->data;
front=front->next;
if(front==NULL) rear=NULL;
free(temp);
return(rv);
}
}
void displayQ()
{
struct Node *temp=front;
while(temp!=NULL)
{
printf(" %d---->",temp->data);
temp=temp->next;
}
printf(" NULL");
}
void main()
{
int ch;
do
{
printf("\nMenu\n----\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
displayQ();
break;
case 2:
int data;
data=dequeue();
if(data!=-1)
printf("The dequed data is:%d\n",data);
displayQ();
break;
case 3:
displayQ();
break;
case 4:
exit(0);
default:
printf("There is no such operation:");
}
}
while(1);
}
Comments
Post a Comment