-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitingList.c
216 lines (192 loc) · 4.68 KB
/
WaitingList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<stdio.h>
struct Passenger
{
int pnr;
char name[25];
int fare;
};
struct waitingListQueue
{
struct Passenger pa[100];
int rear;
int front;
};
struct confirmedListQueue
{
struct Passenger pa[100];
int rear;
int front;
};
int search(struct confirmedListQueue c,int pt);
void enqueueConfirmed(struct confirmedListQueue *pc,struct Passenger p);
void enqueue(struct waitingListQueue *pw,struct Passenger p);
int dequeue(struct waitingListQueue *pw);
void print(struct waitingListQueue w,struct confirmedListQueue c);
int isFull(struct waitingListQueue w);
int isEmpty(struct waitingListQueue w);
void main(){
struct Passenger p;
struct waitingListQueue w;
struct confirmedListQueue c;
int PNRLast = 0;
w.front = -1;
w.rear = -1;
c.front = -1;
c.rear = -1;
printf("---------------Waiting List----------------\n");
int choice = 1;
while(choice>0){
printf("What do you want do?\n");
printf("Add a passenger - 1\n");
printf("Confirm a seat - 2\n");
printf("Check if Full - 3\n");
printf("Check if Empty - 4\n");
printf("Check PNR in Confirmed List - 5\n");
printf("Exit - 0\n");
printf("Enter Option: ");
scanf("%d",&choice);
if(choice==1){
//Enqueue
printf("-------Add Passenger--------\n");
printf("Enter Name of Passenger: ");
scanf("%s",p.name);
printf("Enter fare: ");
scanf("%d",&p.fare);
PNRLast++;
p.pnr = PNRLast;
enqueue(&w,p);
print(w,c);
}
if(choice==2){
//Dequeue
//Storing to be dequeued person details and sending to confirmed list
enqueueConfirmed(&c,w.pa[w.front]);
if(c.rear<99){
int v = dequeue(&w);
if(v < 0)
printf("There are no passengers in the waiting list to confirm seat.\n");
else {
printf("Seat confirmed for %s.\n",c.pa[c.front].name);
}
}
print(w,c);
}
if(choice==3){
//IsFull
int f = isFull(w);
if(f==1)printf("\nThe Waiting list is Full.\n\n");
if(f==0)printf("\nThe Waiting list is not Full.\n\n");
}
if(choice==4){
//IsEmpty
int f2 = isEmpty(w);
if(f2==1)printf("\nThe Waiting list is Empty.\n\n");
if(f2==0)printf("\nThe Waiting list is not Empty.\n\n");
}
if(choice==5){
//Confirmed List search
int pnr = 0;
printf("Enter a PNR to search: ");
scanf("%d",&pnr);
int f2 = search(c,pnr);
if(f2 == -1){
printf("Given PNR %d is NOT found in the confirmed list.\n",pnr);
}
else if(f2 > -1){
printf("Given PNR %d exists in the confirmed list and their details are,\nName: %s\nFare: %d\n",pnr,c.pa[f2].name,c.pa[f2].fare);
}
}
printf("*******************************************\n\n");
}
}
void enqueue(struct waitingListQueue *pw,struct Passenger p){
//Boundary Conditions
if(pw->rear == 99)printf("the waiting list is already full.\n");
//Empty Condition
if(pw->front == -1 && pw->rear == -1){
pw->front++;
pw->rear++;
}
else {
pw->rear++;
}
pw->pa[pw->rear] = p;
}
void enqueueConfirmed(struct confirmedListQueue *pc,struct Passenger p){
//Boundary Conditions
if(pc->rear == 99){
printf("the confirmed list is already full.\n");
pc->rear++;
}
//Empty Condition
else if(pc->front == -1 && pc->rear == -1){
pc->front++;
pc->rear++;
}
else {
pc->rear++;
}
pc->pa[pc->rear] = p;
}
int dequeue(struct waitingListQueue *pw){
int temp=0;
//Boundary Conditions
if(pw->front == -1 && pw->rear == -1)temp=-1;
else {
temp = pw->pa[pw->front].pnr;
pw->front++;
if(pw->rear < pw->front){
//Setting as empty queue
pw->rear = -1;
pw->front = -1;
}
}
return (temp);
}
int isFull(struct waitingListQueue w){
if(w.rear == 99)
return 1;
else
return 0;
}
int isEmpty(struct waitingListQueue w){
if(w.front == -1 && w.rear == -1)
return 1;
else
return 0;
}
int search(struct confirmedListQueue c,int pt){
int rt = -1;
if(c.front > -1 && c.rear > -1){
for(int i=c.front;i<c.rear+1;i++){
if(c.pa[i].pnr == pt){
rt = i;
break;
}
}
}
return rt;
}
void print(struct waitingListQueue w,struct confirmedListQueue c){
//Empty check
if(w.front == -1 && w.rear == -1){
printf("Waiting List is Empty.\n");
}
else {
printf("\nPassengers in the waiting list are,\n");
for(int i=w.front;i<w.rear+1;i++){
printf("Passenger %d :\n\t\tPNR : %d\n\t\tName : %s\n\t\tFare : %d\n",i+1-w.front,w.pa[i].pnr,w.pa[i].name,w.pa[i].fare);
}
printf("\n\n");
}
if(c.front == -1 && c.rear == -1){
printf("Confirmed List is Empty.\n");
}
else {
printf("\nPassengers in the confirmed list are,\n");
for(int j=c.front;j<c.rear+1;j++){
printf("Passenger %d :\n\t\tPNR : %d\n\t\tName : %s\n\t\tFare : %d\n",j+1-c.front,c.pa[j].pnr,c.pa[j].name,c.pa[j].fare);
}
printf("\n\n");
}
}