-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_manage_code
More file actions
180 lines (164 loc) · 3.92 KB
/
Copy pathtrain_manage_code
File metadata and controls
180 lines (164 loc) · 3.92 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
int size;
int top;
char **arr; // Array of strings
};
int isFull(struct stack *ptr)
{
if (ptr->top == ptr->size - 1)
{
printf("TRAIN IS FULL\n");
return -1;
}
return 0;
}
int isEmpty(struct stack *ptr)
{
if (ptr->top == -1)
{
printf("TRAIN IS EMPTY\n");
return 1;
}
return 0;
}
void push(struct stack *ptr, char *val)
{
if (isFull(ptr))
{
printf("TRAIN IS ALREADY FULL, CANNOT ADD MORE PASSENGERS\n");
}
else
{
ptr->top++;
ptr->arr[ptr->top] = (char *)malloc((strlen(val) + 1) * sizeof(char)); // Allocate memory for the string
strcpy(ptr->arr[ptr->top], val); // Copy the passenger name
printf("\n PASSENGER %s HAS BEEN ADDED\n", val);
}
}
char *pop(struct stack *ptr)
{
if (isEmpty(ptr))
{
printf("TRAIN IS EMPTY, YOU CAN BOOK A TICKET NOW\n");
return NULL;
}
else
{
char *val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}
char *peek(struct stack *sp, int i)
{
int arrIndex = sp->top - i + 1;
if (arrIndex < 0 || arrIndex > sp->top)
{
printf("INVALID POSITION\n");
return NULL;
}
else
{
return sp->arr[arrIndex];
}
}
void display(struct stack *sp)
{
if (isEmpty(sp))
{
printf("TRAIN IS EMPTY, NO ONE IS IN THE TRAIN\n");
}
else
{
printf("PASSENGERS ARE:\n");
for (int i = 0; i <= sp->top; i++)
{
printf("%s ", sp->arr[i]);
}
printf("\n");
}
}
int main()
{
struct stack *sp = (struct stack *)malloc(sizeof(struct stack));
sp->size = 10;
sp->top = -1;
sp->arr = (char **)malloc(sp->size * sizeof(char *)); // Allocate memory for an array of strings
char h[50];
int option;
do
{
printf("\n1. Check if train is full\n");
printf("2. Check if train is empty\n");
printf("3. Book a ticket\n");
printf("4. Remove a passenger\n");
printf("5. Last stop of this train\n");
printf("6. Display list of train members\n");
printf("7. Exit\n");
printf("Enter your option: ");
scanf("%d", &option);
getchar(); // Clear the buffer to handle newline
switch (option)
{
case 1:
isFull(sp);
break;
case 2:
isEmpty(sp);
break;
case 3:
if (!isFull(sp))
{
printf("ENTER THE NAME OF THE PASSENGER: \n");
fgets(h, 50, stdin);
h[strcspn(h, "\n")] = 0; // Remove newline character from the input
push(sp, h);
}
break;
case 4:
{
char *passenger = pop(sp);
if (passenger != NULL)
{
printf("PASSENGER %s IS LEAVING THE TRAIN\n", passenger);
free(passenger); // Free the memory allocated for the passenger name
}
break;
}
case 5:
{
printf("Enter the position from the top (1 for last stop): ");
int pos;
scanf("%d", &pos);
getchar(); // Clear the buffer to handle newline
char *passenger = peek(sp, pos);
if (passenger != NULL)
{
printf("Passenger at position %d: %s\n", pos, passenger);
}
break;
}
case 6:
display(sp);
break;
case 7:
printf("Jai Hind! Dhanyawad, Thank you!\n");
break;
default:
printf("Error: Please enter a valid option.\n");
break;
}
} while (option != 7);
// Free allocated memory
for (int i = 0; i <= sp->top; i++)
{
free(sp->arr[i]);
}
free(sp->arr);
free(sp);
return 0;
}