C program in data structure in queue. For character DMA Queue
#include<stdio.h>
#include<conio.h>
#include<process.h>
void insert();
void deletes();
void display();
struct node
{
char info;
struct node *link;
}*front=NULL,*rear=NULL;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**\n");
printf("\n 1. insert \n 2. deletes");
printf("\n 3. display \n 4. exit");
printf("\n Enter your choice:- ");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : deletes();
break;
case 3 : display();
break;
case 4 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
char x;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
}
else
{
printf("\n Enter character:-");
scanf("%c",&x);
temp->info=x;
temp->link=NULL;
if(rear==NULL&&front==NULL)
{
rear=temp;
front=temp;
}
else
{
q=rear;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
}
void deletes()
{
struct node *q;
if(front==NULL&&rear==NULL)
{
printf("\n Queue is empty");
}
else
{
front=rear;
rear=front->link;
printf("\n deleted node is %c",front->info);
free(front);
}
}
void display()
{
struct node *q;
if(front==NULL&&rear==NULL)
{
printf("\n Queue is empty");
}
else
{
front=rear;
while(front!=NULL)
{
printf("\n %c",front->info);
front=front->link;
}
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
void insert();
void deletes();
void display();
struct node
{
char info;
struct node *link;
}*front=NULL,*rear=NULL;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**\n");
printf("\n 1. insert \n 2. deletes");
printf("\n 3. display \n 4. exit");
printf("\n Enter your choice:- ");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : deletes();
break;
case 3 : display();
break;
case 4 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
char x;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Insufficient memory");
}
else
{
printf("\n Enter character:-");
scanf("%c",&x);
temp->info=x;
temp->link=NULL;
if(rear==NULL&&front==NULL)
{
rear=temp;
front=temp;
}
else
{
q=rear;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
}
void deletes()
{
struct node *q;
if(front==NULL&&rear==NULL)
{
printf("\n Queue is empty");
}
else
{
front=rear;
rear=front->link;
printf("\n deleted node is %c",front->info);
free(front);
}
}
void display()
{
struct node *q;
if(front==NULL&&rear==NULL)
{
printf("\n Queue is empty");
}
else
{
front=rear;
while(front!=NULL)
{
printf("\n %c",front->info);
front=front->link;
}
}
}