#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int info;
struct node *next;
}node;
void enqueue(node **head)
{
node *q=*head,*temp;
temp=(node*)malloc(sizeof(node));
printf("\nEnter the element to insert= ");
scanf("%d",&temp->info);
temp->next=NULL;
if(q==NULL)
{
*head=temp;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display(node *head)
{
if(head==NULL)
printf("\nQueue is Empty.");
else
{
do
{
printf("%d ",head->info);
head=head->next;
}while(head!=NULL);
}
}
void dequeue(node **head)
{
node *q=*head;
if(q==NULL)
printf("\nQueue is Empty. Element can't be deleted.");
else
{
printf("\nElement deleted= %d\n",q->info);
*head=q->next;
free(q);
}
}
void main()
{
int choice;
node *start=NULL;
clrscr();
printf("\n\t[1] Enqueue");
printf("\n\t[2] Dequeue");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
do
{
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue(&start);
break;
case 2: dequeue(&start);
break;
case 3: display(start);
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...