-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cpp
238 lines (221 loc) · 4.03 KB
/
queue.cpp
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
/*
* queue.cpp
*
* Created on: 06-May-2021
* Author: sakshi
*/
#include<iostream>
using namespace std ;
class Passenger//class Passenger
{
public:
string name ;
int age ;
char gender ;
friend class Queue ;//friend class Queue
Passenger()
{
name = "" ;
age = 0 ;
gender = '\0' ;
}
void add() ;// add passenger details
void display() ;//display ticket
bool canceltkt(string s) ;//
};
class Queue//class Queue
{
//instance variables
Passenger wait[10] ;
int front ;
int rear ;
public:
Queue()//default constructor
{
front = 0 ;
rear = -1 ;
}
//functions
int isfull() ;
int isempty() ;
void enque() ; //to add passenger to waiting list
Passenger deque() ;
void displaywait() ;
};
void Passenger::add()////function defination of add
{
cout<<"-------------------------------------------------------------------"<<endl;
cout<<"Enter your name: ";
cin>>name;
cout<<"Enter your gender: ";
cin>>gender;
cout<<"Enter your age: ";
cin>>age;
cout<<"-------------------------------------------------------------------"<<endl;
}
void Passenger::display()//function defination of display
{
cout<<"-------------------------------------------------------------------"<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
}
bool Passenger::canceltkt(string s)//function defination of canceltkt
{
bool flag = false ;
if(s == name)
{
cout<<"Your ticket is cancelled"<<endl ;
flag = true ;
}
return flag ;
}
int Queue::isfull()//function defination of isfull
{
if(rear == 9)
{
return 1 ;
}
else
{
return 0 ;
}
}
int Queue::isempty()//function defination of isempty
{
if(front == (rear + 1))
{
return 1 ;
}
else
{
return 0 ;
}
}
void Queue::enque()//function defination of enque to add passenger to waiting list
{
if(isfull() == 0)
{
cout<<"-------------------------------------------------------------------"<<endl;
cout<<endl;
cout<<"You are being put to waiting list"<<endl;
cout<<endl;
rear ++ ;
wait[rear].add() ;//adding to waiting list
}
else
{
cout<<endl;
cout<<"No further reservations are possible...."<<endl;
cout<<endl;
}
}
Passenger Queue::deque()//function defination of deque
{
Passenger a ;
if(isempty() == 1)
{
cout<<"No passengers in waiting list......"<<endl;
}
else
{
a = wait[front] ;//deleting from waiting list
front ++ ;
}
return a ;
}
void Queue::displaywait()
{
if(isempty()==1)
{
cout<<" No Registrations"<<endl ;
}
else
{
for(int i = front ; i <= rear ; i++)
{
wait[i].display() ;//display
}
}
}
int main()
{
Passenger h[5] ;
Queue f ;
int pos=0;
string s;
Passenger m;
int cnt = 0 ;
int ch ;
do
{
//display menu
cout<<endl;
cout<<"Railway Rservation System"<<endl;
cout<<"MENU : "<<endl;
cout<<"\n" ;
cout<<"1.Book Ticket"<<endl;
cout<<"2.Cancel Ticket"<<endl;
cout<<"3.Confirm Ticket List"<<endl ;
cout<<"4.Waiting list"<<endl ;
cout<<"0.EXIT"<<endl;
cout<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
if(cnt < 5)
{
cout<<"\n";
cout<<"Passenger No. :"<<cnt+1<<endl ;
h[cnt].add() ;
cout<<"Your Ticket is confirmed"<<endl ;
cnt++ ;
}
else
{
f.enque() ;//calling enque to add passengers in waiting list
}
break ;
case 2:
cout<<"Enter name of the passenger: ";
cin>>s;
for(int i=0;i<5;i++)
{
if(h[i].canceltkt(s)==true)//canceling ticket
{
pos = i;
break;
}
}
cout<<"\n" ;
m = f.deque();//calling deque to delete passenger from waiting list
//inserting the passenger in confirmed ticket list
h[pos].name = m.name;
h[pos].gender = m.gender;
h[pos].age = m.age;
break ;
case 3 :
cout<<"CONFIRM RESERVATIONS: "<<endl;
cout<<endl;
for(int j=0;j<5;j++)
{
h[j].display();//display confirmed list
}
cout<<endl;
break ;
case 4:
cout<<"WAITING LIST: "<<endl;
f.displaywait();//display waiting list
break;
case 0:
cout<<"Thank you !!"<<endl ;
break ;
default:
cout<<"Invalid choice"<<endl ;
break ;
}
}while(ch != 0) ;
}