-
Notifications
You must be signed in to change notification settings - Fork 10
/
railway reservation.c
250 lines (212 loc) · 4.09 KB
/
railway reservation.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define size 2
typedef struct NODE
{
int reg_no;
int age;
char name[20];
struct NODE *next;
} node;
node* deq();
int create();
int reserve(node*);
int cancel(int);
void enq(node*);
void display();
node *start;
node *front;
node *rear;
int count=0;
int num=0;
int create( )
{
node *new;
new=(node *)malloc(sizeof(node));
new->reg_no=1;
printf("Name: ");
scanf("%s", new->name);
printf("Age : ");
scanf("%d", &new->age);
if(new->age>=90 || new->age<=10) {
free(new);
return -1;
}
start=new;
new->next=NULL;
num++;
return 1;
}
int reserve(node *start)
{
int temp;
if(start==NULL)
{
temp = create(start);
return temp;
}
else
{
node *temp, *new_node;
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
new_node=(node *)malloc(sizeof(node));
printf("Name: ");
scanf("%s", new_node->name);
printf("Age : ");
scanf("%d", &new_node->age);
if(new_node->age >=90 || new_node->age<=10) {
return -1;
}
new_node->next=NULL;
if(num<=size)
{
num++;
new_node->reg_no=num;
temp->next=new_node;
return 1;
}
else
{
enq(new_node);
return 0;
}
}
}
int cancel(int reg)
{
node *ptr, *preptr, *new;
ptr=start;
preptr=NULL;
if(start==NULL)
return -1;
if(ptr->next==NULL && ptr->reg_no==reg)
{
start=NULL;
num--;
free(ptr);
return 1;
}
else{
while(ptr->reg_no!=reg && ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
if(ptr==NULL && ptr->reg_no!=reg)
return -1;
else
preptr->next=ptr->next;
free(ptr);
new=deq();
while(preptr->next!=NULL)
preptr=preptr->next;
preptr->next=new;
num--;
return 1;
}
}
void enq(node *new_node)
{
if(rear==NULL)
{
rear=new_node;
rear->next=NULL;
front=rear;
}
else
{
node *temp;
temp=new_node;
rear->next=temp;
temp->next=NULL;
rear=temp;
}
count++;
}
node* deq()
{
node *front1;
front1=front;
if(front==NULL)
return NULL;
else
{
count-- ;
if(front->next!=NULL)
{
front=front->next;
front1->next=NULL;
return front1;
}
else
{
front=NULL;
rear=NULL;
return front1;
}
}
}
void display()
{
node *temp;
temp=start;
while(temp!=NULL)
{
printf("\nRegistration Number: %d\n", temp->reg_no);
printf("Name : %s\n\n", temp->name);
temp=temp->next;
}
}
int main()
{
int choice, status=0,canc=0, reg=0;
start=NULL;
rear=NULL;
front=NULL;
printf("\t\t\t**RAILWAY RESERVATION**\t\t\t\t\n");
int ch =0;
while(ch!=4)
{
printf("\n\nDo you want to - \n 1. Book a ticket \n 2. Cancel Booking \n 3. Display passenger details \n 4. exit\n\n");
scanf("%d", &choice);
switch(choice)
{
case 1 : status=reserve(start);
if(status==0)
printf("\nBooking Full!! \nYou are added to waiting list. Waiting list number %d", count);
else if(status == -1) {
printf("\n age not eligible");
}
else
printf(" \nBooking Successful!!! Enjoy your journey! Your Reg No is %d\n\n", num);
break;
case 2: printf(" \n Give the Registration number to be cancelled\n");
scanf(" %d", ®);
if(reg>num)
printf("Invalid!!");
else
{
canc=cancel(reg);
if(canc==-1)
printf("\nRegistration number invalid!!\n");
else
printf("\nRegistration cancelled successfully\n");
}
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong choice!\n");
}
}
}