/*Serial No.145 [swami127.cpp]*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int info;
struct node *next;
}node;
void enqueue(node **f)
{
node *q=*f,*temp=(node*)malloc(sizeof(node));
printf("\nEnter the element to be inserted= ");
scanf("%d",&temp->info);
if(*f==NULL) /*if inserted node is first node*/
*f=temp;
else /*if inserted node is not first node*/
{
while(q->next!=*f)
{
q=q->next;
}
q->next=temp;
}
temp->next=*f;
}
void display(node *f)
{
node *q=f;
if(f==NULL)
printf("\nCircular Queue is Empty.");
else
{
do
{
printf("%d ",q->info);
q=q->next;
}while(q!=f);
}
}
void dequeue(node **f)
{
node *q=*f,*temp;
if(*f==NULL)
{
printf("\nQueue is Empty. Value can't be deleted.");
return;
}
if(q->next==q) /*if it is single node*/
{
temp=q;
*f=NULL;
free(temp);
return;
}
else /*if more than one node are present.*/
{
temp=q;
while(q->next!=*f)
{
q=q->next;
}
*f=temp->next;
q->next=*f;
free(temp);
}
}
void main()
{
int choice;
node *front=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(&front);
break;
case 2: dequeue(&front);
break;
case 3: display(front);
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...