Question
I need a program in C language using queue with linked list that shows real life application for example queing for train ticket . This
I need a program in C language using queue with linked list that shows real life application for example queing for train ticket . This is queue using array instead of linked list . can someone do it using linked list . Only in C please thanks !
#include
#include
struct reserve
{
struct passenger
{
int seatno,age;
char na[15];
}p[10][5];
struct detail
{
int h,t,count,sr;
}d[10];
}r;
void c_reserve()
{
for(int v=0;v<10;v++)
{
r.d[v].count=0;
r.d[v].h=0;
r.d[v].sr=0;
r.d[v].t=-1;
}
}
int isqueueempty(int n3)
{
return(r.d[n3].count==0?1:0);
}
int isqueuefull(int n3)
{
return(r.d[n3].count==(3+r.d[n3].sr)?1:0);
}
void disp()
{
printf("1.SEDHU EXPRESS FROM:KL TO: IPOH ");
printf("2.BAGATH EXPRESS FROM:TAIPEI TO: KL ");
printf("3.HOWRAH EXPRESS FROM:BATU GAJAH TO: KL ");
printf("4.RAJDHANI EXPRESS FROM:KL TO: SUNGAI PETANI ");
printf("5.PANDIAN EXPRESS FROM:KL TO: BUTTERWORTH ");
printf("------Enter your choice----- ");
}
int enqueue(int t1)
{
int i,m1,s,g,u;
char ch,ch1;
if(t1==-1)
{
disp();
printf("Enter the row which is to be enqueued:");
scanf("%d",&i);
i=i-1;
m1=i;
t1++;
}
else
{
m1=t1;
i=5;
}
s=t1+i;
u=5+t1;
if(r.d[s].sr<2)
{
printf("Do you belong to special reservation category say y or n: ");
scanf("%c",&ch);
if(ch=='y')
r.d[s].sr++;
}
if(!isqueuefull(s)||!isqueuefull(m1))
{
if(isqueueempty((5+m1))||(isqueuefull(m1)&&!isqueueempty((5+m1))))
{
r.d[s].t=(r.d[s].t+1)%5;
r.d[s].count++;
printf("Enter the name of the passenger:");
scanf("%s",r.p[s][r.d[s].t].na);
printf("Enter the age of the passenger:");
scanf("%d",&r.p[s][r.d[s].t].age);
if(r.p[s][r.d[s].t].age>110)
{
printf("INVALID DATA Enter the age of the passenger:");
scanf("%d",&r.p[s][r.d[s].t].age);
}
printf("Enter the ticket number of the passenger:");
scanf("%d",&r.p[s][r.d[s].t].seatno);
}
else
{
r.d[t1].t=(r.d[t1].t+1)%5;
strcpy(r.p[t1][r.d[t1].t].na,r.p[u][r.d[u].h].na);
r.p[t1][r.d[t1].t].age=r.p[u][r.d[u].h].age;
r.p[t1][r.d[t1].t].seatno=r.p[u][r.d[u].h].seatno;
printf(" %s is removed from the waiting list",r.p[u][r.d[u].h].na);
r.d[u].h=(r.d[u].h+1)%5;
r.d[u].count--;
r.d[t1].count++;
}
}
else
{
printf("Queue is full. You cannot reserve......");
if(!isqueuefull(5+m1))
{
printf("Do you want to be in the waiting list:");
scanf("%c",&ch1);
if(ch1=='y')
g=enqueue(m1);
}
}
return r.d[s].t;
}
int dequeue()
{
int n2,g;
printf("Enter the queue which has to be dequeued:");
disp();
scanf("%d",&n2);
n2=n2-1;
if(!isqueueempty(n2))
{
printf("Service is given to %s first",r.p[n2][r.d[n2].h].na);
r.d[n2].h=(r.d[n2].h+1)%5;
r.d[n2].count=r.d[n2].count-1;
if(!isqueueempty(n2+5))
g=enqueue(n2);
}
else
printf("There are no passengers. You cannot provide service.");
return r.d[n2].h;
}
void display(int i1)
{
i1--;
int i=r.d[i1].h,j=1;
printf("=======================================================");
printf("NAME: AGE: SEATNO:");
while(j<=r.d[i1].count)
{
printf(" %s %d %d ",r.p[i1][i].na,r.p[i1][i].age,r.p[i1][i].seatno);
i=(i+1)%5;
j++;
}
printf("=======================================================");
}
int main()
{
int n,nn=1,m,t=0,k;
char ch;
c_reserve();
while(nn==1)
{
printf("Do you want to reserve or cancel Enter either 1 or 2:");
scanf("%d",&n);
switch(n)
{
case 1:
k=enqueue(-1);
break;
case 2:
k=dequeue();
break;
case 3:
printf("Do you want to display all say y or n (if waiting list press w): ");
scanf("%c",&ch);
if(ch=='y')
{
m=0;
while(m<5)
{
display(m);
m++;
}
}
else
{
disp();
printf("Enter the choice :");
scanf("%d",&m);
}
if(ch=='w')
display(5+m);
if(ch=='n')
display(m);
break;
}
printf(" do you want to continue press 1:");
scanf("%d",&nn);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started