Queue using linked list cprogram
Aim:Implementation of queue using linked list in C program
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void enque(int);
void deque();
void display();
struct queue
{
int data;
struct queue *next;
};
typedef struct queue node;
node *rear,*front,*new,*dfront;
void main()
{
int ch,item;
clrscr();
front=rear=NULL;
while(1)
{
printf("1.enque\t2.deque\t3.display\t4.exit\n");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element to insert");
scanf("%d",&item);
enque(item);
break;
case 2:
deque();
break;
case 3:
printf("elements in queue are: ");
display();
break;
default :
exit(0);
}
}
}
void enque(int item)
{
if(rear==NULL)
{
rear=(node *)malloc(sizeof(node));
rear->data=item;
rear->next=NULL;
front=rear;
}
else
{
new=(node *)malloc(sizeof(node));
new->data=item;
new->next=NULL;
rear->next=new;
rear=new;
}
}
void deque()
{
if(front==NULL)
printf("underflow\n");
else
{
printf("element deleted is %d\n",front->data);
if(front==rear)
front=rear=NULL;
else
front=front->next;
}
}
void display()
{ dfront=front;
while(dfront!=NULL)/*dfront means duplicate front*/
{
printf("%d ",dfront->data);
dfront=dfront->next;
}
printf("\n");
}
Output:
No comments:
Post a Comment