-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab7.c
More file actions
236 lines (207 loc) · 6.64 KB
/
lab7.c
File metadata and controls
236 lines (207 loc) · 6.64 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Structure to represent a hotel room
typedef struct Room {
int roomNumber;
char guestName[50];
struct Room *left, *right;
} Room;
// Function prototypes
Room* createRoom(int roomNumber, const char* guestName);
Room* insertRoomLevelOrder(Room* root, int roomNumber, const char* guestName);
void printInOrder(Room* root);
void printPreOrder(Room* root);
void printPostOrder(Room* root);
void displayTreeStructure(Room* root, int level);
void displayMenu();
void saveRoomsToFile(Room* root, const char* fileName);
Room* loadRoomsFromFile(const char* fileName);
void freeTree(Room* root);
int main() {
Room* hotelRooms = NULL;
char fileName[] = "hotel_data.txt";
int choice;
do {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
int roomNumber;
char guestName[50];
printf("Enter room number: ");
scanf("%d", &roomNumber);
printf("Enter guest name: ");
scanf("%s", guestName);
hotelRooms = insertRoomLevelOrder(hotelRooms, roomNumber, guestName);
printf("Room added successfully.\n");
break;
}
case 2:
printf("In-order Traversal:\n");
printInOrder(hotelRooms);
printf("\n");
break;
case 3:
printf("Pre-order Traversal:\n");
printPreOrder(hotelRooms);
printf("\n");
break;
case 4:
printf("Post-order Traversal:\n");
printPostOrder(hotelRooms);
printf("\n");
break;
case 5:
displayTreeStructure(hotelRooms, 0);
break;
case 6:
saveRoomsToFile(hotelRooms, fileName);
printf("Hotel rooms data saved to file.\n");
break;
case 7:
hotelRooms = loadRoomsFromFile(fileName);
if (hotelRooms != NULL) {
printf("Hotel rooms data loaded from file.\n");
} else {
printf("Error loading data from file.\n");
}
break;
case 8:
freeTree(hotelRooms);
printf("Memory freed. Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 8);
return 0;
}
// Function to create a new hotel room
Room* createRoom(int roomNumber, const char* guestName) {
Room* newRoom = (Room*)malloc(sizeof(Room));
if (newRoom == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(EXIT_FAILURE);
}
newRoom->roomNumber = roomNumber;
strcpy(newRoom->guestName, guestName);
newRoom->left = newRoom->right = NULL;
return newRoom;
}
// Function to insert a hotel room in level order
Room* insertRoomLevelOrder(Room* root, int roomNumber, const char* guestName) {
if (root == NULL) {
return createRoom(roomNumber, guestName);
}
// Create an array to hold the nodes at the last level
Room** nodes = (Room**)malloc(sizeof(Room*) * 100);
int front = -1, rear = -1;
nodes[++rear] = root;
while (front != rear) {
Room* temp = nodes[++front];
if (temp->left == NULL) {
temp->left = createRoom(roomNumber, guestName);
free(nodes);
return root;
} else {
nodes[++rear] = temp->left;
}
if (temp->right == NULL) {
temp->right = createRoom(roomNumber, guestName);
free(nodes);
return root;
} else {
nodes[++rear] = temp->right;
}
}
free(nodes);
return root;
}
// Function to perform in-order traversal
void printInOrder(Room* root) {
if (root != NULL) {
printInOrder(root->left);
printf("Room %d: %s\n", root->roomNumber, root->guestName);
printInOrder(root->right);
}
}
// Function to perform pre-order traversal
void printPreOrder(Room* root) {
if (root != NULL) {
printf("Room %d: %s\n", root->roomNumber, root->guestName);
printPreOrder(root->left);
printPreOrder(root->right);
}
}
// Function to perform post-order traversal
void printPostOrder(Room* root) {
if (root != NULL) {
printPostOrder(root->left);
printPostOrder(root->right);
printf("Room %d: %s\n", root->roomNumber, root->guestName);
}
}
// Function to display tree structure
void displayTreeStructure(Room* root, int level) {
if (root != NULL) {
displayTreeStructure(root->right, level + 1);
for (int i = 0; i < level; i++) {
printf("\t");
}
printf("Room %d: %s\n", root->roomNumber, root->guestName);
displayTreeStructure(root->left, level + 1);
}
}
// Function to display menu
void displayMenu() {
printf("\n----- Hotel Management System Menu -----\n");
printf("1. Add a new room\n");
printf("2. Display rooms (In-order Traversal)\n");
printf("3. Display rooms (Pre-order Traversal)\n");
printf("4. Display rooms (Post-order Traversal)\n");
printf("5. Display tree structure\n");
printf("6. Save rooms data to file\n");
printf("7. Load rooms data from file\n");
printf("8. Exit\n");
}
// Function to save hotel rooms data to a file
void saveRoomsToFile(Room* root, const char* fileName) {
FILE* file = fopen(fileName, "w");
if (file == NULL) {
printf("Error opening file for writing. Exiting...\n");
exit(EXIT_FAILURE);
}
if (root != NULL) {
fprintf(file, "%d %s\n", root->roomNumber, root->guestName);
saveRoomsToFile(root->left, fileName);
saveRoomsToFile(root->right, fileName);
}
fclose(file);
}
// Function to load hotel rooms data from a file
Room* loadRoomsFromFile(const char* fileName) {
FILE* file = fopen(fileName, "r");
if (file == NULL) {
printf("Error opening file for reading. Returning NULL...\n");
return NULL;
}
Room* root = NULL;
int roomNumber;
char guestName[50];
while (fscanf(file, "%d %s", &roomNumber, guestName) == 2) {
root = insertRoomLevelOrder(root, roomNumber, guestName);
}
fclose(file);
return root;
}
// Function to free memory allocated for the tree
void freeTree(Room* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}