Write c program to perform following function on Dynamic queue 1) Insert 2) Delete 3) Display
Dynamic Queue
Insert into Queue
Delete from queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//To create structure
struct que
{
int data;
struct que *next;
};
typedef struct que node;
//To define functions
void insertq(node **, node **);
int deleteq(node **, node **);
int isempty(node *, node*);
void display(node *, node *);
void main()
{
int ch,data;
node *f=NULL,*r=NULL;
do
{
printf("\n1.Insert Queue \n2.Display \n3.Delete \nOther than above EXIT.");
printf("\nEnter your choice.");
scanf("%d",&ch);
switch (ch)
{
case 1:
insertq(&f,&r);
break;
case 2:
display(f,r);
break;
case 3:
if(isempty(r,f))
printf("\nQueue is EMPTY.");
else
{
data=deleteq(&f,&r);
printf("\n%d is removed from queue.",data);
}
break;
}
}while(ch<=3&&ch>=1);
}
void insertq(node **f, node**r)
{
node *temp=NULL;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data: ");
scanf("%d",&temp->data);
temp->next=NULL;
if(*r==NULL&&*f==NULL)
*r=*f=temp;
else
{
(*r)->next=temp;
*r=temp;
}
}
//To check queue is empty or not
int isempty(node *f,node *r)
{
if(r==NULL&&f==NULL)
return 1;
else
return 0;
}
//To delete elements in queue
int deleteq(node **f, node **r)
{
node *ptr;
int data;
ptr=*f;
if(*f==*r)
*f=*r=NULL;
else
{
*f=(*f)->next;
}
data=ptr->data;
ptr->next=NULL;
free(ptr);
return data;
}
//To display elements in queue
void display(node *f, node *r)
{
node *ptr=NULL;
if(isempty(f,r))
printf("\nQueue is EMPTY.");
else
{
ptr=f;
printf("\nContent of queue is: ");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}
0 Comments