-
Notifications
You must be signed in to change notification settings - Fork 0
/
Railway.cpp
181 lines (177 loc) · 4 KB
/
Railway.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
#include <iostream>
const int size =5;
using namespace std;
int i, mai[size], wai[size], front = -1, rear = -1, front2 = -1, rear2 = -1,ind=-1;
void book(int id) {
if (front == (rear + 1) % size) {
cout<<" ";
cout << "The main queue is full, You will be placed in the waiting list" << endl;
if (front2 == (rear2 + 1) % size) {
cout << "The waiting queue is full" << endl;
}
else {
if (front2 == -1) {
front2 = rear2 = 0;
wai[rear2] = id;
}
else {
rear2 = (rear2 + 1) % size;
wai[rear2] = id;
}
}
}
else {
if (front == -1) {
front = rear = 0;
mai[rear] = id;
}
else {
rear = (rear + 1) % size;
mai[rear] = id;
}
}
}
void fir(){
cout<<"the first person to get the ticket is: "<<mai[front]<<endl;
cout<<"The first person of the waiting list is: "<<wai[front2]<<endl;
}
void cancel() {
if (front == -1) {
cout << "No ID to recieve ticket" << endl;
}
else {
if (front == rear) {
front = rear = -1;
}
else {
front = (front + 1) % size;
}
if (front2 != -1) {
mai[rear] = wai[front2];
rear = (rear + 1) % size;
front2 = (front2 + 1) % size;
}
}
}
void del(int id){
if(front!=-1){
cout<<"Enter the id you want to delete"<<endl;
cin>>id;
if(front<rear){
for(i=front;i<=rear;i++){
if(mai[i]==id){
ind = i;
}
}
}
else{
for(i=front;i<=size-1;i++){
if(mai[i]==id){
ind=i;
}
}
for(i=0;i<=rear;i++){
if(mai[i]==id){
ind=i;
}
}
}
if(ind==-1){
cout<<"\nInvalid id please enter a valid id"<<endl;
}
if(front<rear){
for(i=ind;i<=rear;i++){
mai[i]=mai[(i+1)%size];
}
if(front2!=-1){
mai[rear]=wai[front2];
front2++;
}
else{
rear--;
}
}
else{
if(ind>front){
for(i=ind;i<size;i++){
mai[i]=mai[(i+1)%size];
}
for(i=0;i<=rear;i++){
mai[i]=mai[(i+1)%size];
}
if(front2!=-1){
mai[rear]=wai[front2];
front++;
}
else{
rear--;
}
}
}
}
else{
cout<<"No bookings to cancel"<<endl;
}
}
void display() {
cout << "The main queue is: " << endl;
if (front <= rear) {
for (i = front; i <= rear; i++) {
cout << mai[i] << " ";
}
}
else {
for (i = front; i < size; i++) {
cout << mai[i] << " ";
}
for (i = 0; i <= rear; i++) {
cout << mai[i] << " ";
}
}
cout << endl;
cout << "The waiting queue is: " << endl;
if (front2 != -1) {
if (front2 <= rear2) {
for (i = front2; i <= rear2; i++) {
cout << wai[i] << " ";
}
}
else {
for (i = front2; i < size; i++) {
cout << wai[i] << " ";
}
for (i = 0; i <= rear2; i++) {
cout << wai[i] << " ";
}
}
}
cout << endl;
}
int main() {
int m, id;
do {
cout << "\n1 to book\n2 if ticket recieved\n3 to display\n4 to see first ID\n5 to cancel\n6 to exit" << endl;
cin >> m;
if (m == 1) {
cout << "Enter passenger ID: ";
cin >> id;
book(id);
}
else if (m == 2) {
cancel();
}
else if (m == 3) {
display();
}
else if(m==4){
fir();
}
else if(m==5){
del(id);
}
else if (m != 6) {
cout << "Invalid option. Please choose a valid option." << endl;
}
} while (m != 6);
return 0;
}