0% found this document useful (0 votes)
26 views20 pages

- Trần Chí Hữu - Ctdl09

The document contains C code that defines functions for creating, manipulating, and deleting nodes in a linked list data structure. It includes functions to initialize a linked list, add nodes to the head and other positions, delete nodes from the head or other positions, print the linked list, and delete the entire linked list. Main tests the functions by creating some linked lists with randomly generated node values, printing them, deleting nodes, and printing again.

Uploaded by

tranhuu181924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views20 pages

- Trần Chí Hữu - Ctdl09

The document contains C code that defines functions for creating, manipulating, and deleting nodes in a linked list data structure. It includes functions to initialize a linked list, add nodes to the head and other positions, delete nodes from the head or other positions, print the linked list, and delete the entire linked list. Main tests the functions by creating some linked lists with randomly generated node values, printing them, deleting nodes, and printing again.

Uploaded by

tranhuu181924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

23:53 16/01/2024 test.

test.c

1 // BAI 1
2 #include<stdio.h>
3 #include<stdlib.h>
4
5 typedef struct NodeType{
6 int data;
7 Node* next;
8 }Node;
9
10 typedef struct LinkedListType{
11 Node* head;
12 }LinkedList;
13
14 void init(LinkedList* list)
15 {
16 list->head = NULL;
17 }
18
19 Node *makeNode(int data)
20 {
21 Node* new_node = (Node*) malloc(sizeof(Node));
22 new_node->data = data;
23 new_node->next = NULL;
24 return new_node;
25 }
26
27 Node *deleteHead(LinkedList *list)
28 {
29 Node *node = list->head;
30 if (node == NULL)
31 {
32 printf("The linked list is empty.\n");
33 return NULL;
34 }
35 else
36 {
37 list->head = node->next;
38 return node;
39 }
40 }
41
42 void deleteLinkedList(LinkedList *list)
43 {
44 while(list->head != NULL) {
45 Node *temp = deleteHead(list);
46 free(temp);
47 }
48 }
49
50 void insertHead(int data, LinkedList *list)
51 {
52 Node *newNode = makeNode(data);
53 if (list->head == NULL)

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 1/20
23:53 16/01/2024 test.c
54 {
55 list->head = newNode;
56 }
57 else
58 {
59 newNode->next = list->head;
60 list->head = newNode;
61 }
62 }
63
64 void printList(LinkedList *list)
65 {
66 Node *node = list->head;
67 while (node != NULL)
68 {
69 printf("Node address: %p | ", node);
70 printf("data = %d| ", node->data);
71 printf("next node address = %p\n", node->next);
72 node = node->next;
73 }
74 printf("\n");
75 }
76
77 void insertNewNodeAtPositionK(LinkedList *list, int data, int k)
78 {
79 if (k == 1) {
80 insertHead(data, list);
81 return;
82 }
83 if (k < 1 || k > 1 && list->head == NULL) {
84 printf("Can't insert node at k!");
85 return;
86 }
87 Node *curr = list->head;
88 int i = 1;
89 while (i < k - 1) {
90 if (curr->next == NULL) {
91 printf("Can't insert node at k!\n");
92 return;
93 }
94 curr = curr->next;
95 i++;
96 }
97 Node *new_node = makeNode(data);
98 new_node->next = curr->next;
99 curr->next = new_node;
100 }
101
102 Node *deleteNodeAtPositionK(LinkedList *list, int k)
103 {
104 if (k < 1 || list->head == NULL) {
105 printf("Can't delete node at k!\n");
106 return NULL;
107 }
108 if (k == 1) {
109 return deleteHead(list);

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 2/20
23:53 16/01/2024 test.c
110 }
111 Node* temp = list->head;
112 int i = 1;
113 while (i < k - 1) {
114 if (temp->next == NULL) {
115 printf("Can't delete node at k!\n");
116 return NULL;
117 }
118 temp = temp->next;
119 i++;
120 }
121 Node *deleted_node = temp->next;
122 temp->next = deleted_node->next;
123 return deleted_node;
124 }
125
126 int main()
127 {
128 LinkedList list;
129 init(&list);
130 insertNewNodeAtPositionK(&list, 2, 1);
131 insertNewNodeAtPositionK(&list, 3, 1);
132 insertNewNodeAtPositionK(&list, 1, 2);
133 printList(&list);
134 Node *temp = deleteNodeAtPositionK(&list, 2);
135 free(temp);
136 printList(&list);
137 deleteLinkedList(&list);
138 return 0;
139 }
140
141
142 // BAI 2
143 #include<stdio.h>
144 #include<stdlib.h>
145 #include<time.h>
146
147 #define N 10
148
149 typedef struct NodeType{
150 int data;
151 struct NodeType* next;
152 }Node;
153
154 typedef struct LinkedListType{
155 Node* head;
156 }LinkedList;
157
158 void init(LinkedList* list)
159 {
160 list->head = NULL;
161 }
162
163 Node *makeNode(int data)
164 {
165 Node* new_node = (Node*) malloc(sizeof(Node));

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 3/20
23:53 16/01/2024 test.c
166 new_node->data = data;
167 new_node->next = NULL;
168 return new_node;
169 }
170
171 Node *deleteHead(LinkedList *list)
172 {
173 Node *node = list->head;
174 if (node == NULL)
175 {
176 printf("The linked list is empty.\n");
177 return NULL;
178 }
179 else
180 {
181 list->head = node->next;
182 return node;
183 }
184 }
185
186 void deleteLinkedList(LinkedList *list)
187 {
188 while(list->head != NULL) {
189 Node *temp = deleteHead(list);
190 free(temp);
191 }
192 }
193
194 void insertHead(int data, LinkedList *list)
195 {
196 Node *newNode = makeNode(data);
197 if (list->head == NULL)
198 {
199 list->head = newNode;
200 }
201 else
202 {
203 newNode->next = list->head;
204 list->head = newNode;
205 }
206 }
207
208 void printList(LinkedList *list)
209 {
210 Node *node = list->head;
211 while (node != NULL)
212 {
213 printf("Node address: %p\t|", node);
214 printf("data = %d\t| ", node->data);
215 printf("next node address = %p\n", node->next);
216 node = node->next;
217 }
218 printf("\n");
219 }
220
221 void insert(LinkedList *list, int data)

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 4/20
23:53 16/01/2024 test.c
222 {
223 if (list->head == NULL) {
224 insertHead(data, list);
225 return;
226 }
227 Node *new_node = makeNode(data);
228 Node *prev = list->head;
229 Node *curr;
230 while (prev->next != NULL && data < prev->data) {
231 curr = prev->next;
232 if (data >= curr->data) {
233 new_node->next = curr;
234 prev->next = new_node;
235 return;
236 }
237 prev = prev->next;
238 }
239 if (data <= prev->data) {
240 prev->next = new_node;
241 }
242 else {
243 insertHead(data, list);
244 }
245 }
246
247 void delete(LinkedList *list, int value)
248 {
249 Node *node = NULL;
250 Node *curr = list->head;
251 if (curr == NULL) {
252 return;
253 }
254 if (curr->data == value) {
255 free(deleteHead(list));
256 return;
257 }
258 node = curr->next;
259 while (node->next != NULL && value > node->data) {
260 curr = node;
261 node = node->next;
262 }
263 if (node->data == value) {
264 if (node->next == NULL) {
265 curr->next = NULL;
266 free(node);
267 }
268 else {
269 curr->next = node->next;
270 free(node);
271 }
272 }
273 }
274
275 int main()
276 {
277 srand(time(0));

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 5/20
23:53 16/01/2024 test.c
278 LinkedList a;
279 init(&a);
280 for (int i = 0; i < N; i++) {
281 insert(&a, -2*N + rand()%(4*N + 1));
282 }
283 printf("The linked list is:\n");
284 printList(&a);
285 deleteLinkedList(&a);
286
287 LinkedList b;
288 init(&b);
289
290 int arr[N];
291 arr[0] = -2*N + rand()%(2*N);
292 for (int i = 1; i < N; i++) {
293 arr[i] = arr[i - 1] + rand()%N;
294 }
295 for (int i = N - 1; i >= 0; i--) {
296 insertHead(arr[i], &b);
297 }
298
299 printList(&b);
300 int value = 10;
301 printf("%d\n", value);
302
303 delete(&b, value);
304
305 printList(&b);
306 deleteLinkedList(&b);
307 return 0;
308 }
309
310
311 // BAI 3
312 #include<stdio.h>
313 #include<stdlib.h>
314 #include<time.h>
315
316 typedef struct NodeType{
317 int data;
318 struct NodeType* next;
319 } Node;
320
321 typedef struct StackType{
322 Node* top;
323 } Stack;
324
325 void init_stack(Stack *stack)
326 {
327 stack->top = NULL;
328 }
329
330 Node *makeNode(int data)
331 {
332 Node* new_node = (Node*) malloc(sizeof(Node));
333 new_node->data = data;

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 6/20
23:53 16/01/2024 test.c
334 new_node->next = NULL;
335 return new_node;
336 }
337
338 int isEmptyStack(Stack *st)
339 {
340 if (st->top == NULL) {
341 return 1;
342 }
343 return 0;
344 }
345
346 void push(Stack *st, int value)
347 {
348 Node *new_node = makeNode(value);
349 if (st->top == NULL) {
350 st->top = new_node;
351 }
352 else {
353 new_node->next = st->top;
354 st->top = new_node;
355 }
356 }
357
358 int pop(Stack *st)
359 {
360 if (isEmpty(st)) {
361 printf("Error: popping from an empty stack\n");
362 return 0;
363 }
364 int top_value = st->top->data;
365 Node *temp = st->top;
366 st->top = st->top->next;
367 free(temp);
368 return top_value;
369 }
370
371 int peek(Stack *st) {
372 if (isEmpty(st)) {
373 printf("Error: trying to peek at an empty stack.\n");
374 return 0;
375 }
376 return st->top->data;
377 }
378
379 typedef struct QueueType {
380 Node *head;
381 Node *tail;
382 } Queue;
383
384 void init_queue(Queue *q)
385 {
386 q->head = NULL;
387 q->tail = NULL;
388 }
389

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 7/20
23:53 16/01/2024 test.c
390 int isEmptyQueue(Queue *q)
391 {
392 if (q->head == NULL) {
393 return 1;
394 }
395 return 0;
396 }
397
398 void put(Queue *q, int value)
399 {
400 Node* new_node = makeNode(value);
401 if (isEmptyQueue(q)) {
402 q->head = new_node;
403 q->tail = new_node;
404 }
405 else {
406 q->tail->next = new_node;
407 q->tail = new_node;
408 }
409 }
410
411 int get(Queue *q)
412 {
413 if (isEmptyQueue(q)) {
414 printf("Error: trying to get from an empty queue.\n");
415 return 0;
416 }
417 Node* old_head = q->head;
418 int value = old_head->data;
419 q->head = old_head->next;
420 free(old_head);
421 if (isEmptyQueue(q)) {
422 q->tail = NULL;
423 }
424 return value;
425 }
426
427 int front(Queue *q)
428 {
429 if (isEmptyQueue(q)) {
430 printf("Error: trying to get the front of an empty queue.\n");
431 return 0;
432 }
433 return q->head->data;
434 }
435
436 int main()
437 {
438 return 0;
439 }
440
441
442 // BAI 4
443 #include<stdio.h>
444 #include<stdlib.h>
445

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 8/20
23:53 16/01/2024 test.c
446
447 typedef struct NodeType{
448 int data;
449 struct NodeType* next;
450 }Node;
451
452 typedef struct LinkedListType{
453 Node* head;
454 }LinkedList;
455
456 void init(LinkedList* list)
457 {
458 list->head = NULL;
459 }
460
461 Node *makeNode(int data)
462 {
463 Node* new_node = (Node*) malloc(sizeof(Node));
464 new_node->data = data;
465 new_node->next = NULL;
466 return new_node;
467 }
468
469 void insertHead(int data, LinkedList *list)
470 {
471 Node *newNode = makeNode(data);
472 if (list->head == NULL)
473 {
474 list->head = newNode;
475 }
476 else
477 {
478 newNode->next = list->head;
479 list->head = newNode;
480 }
481 }
482
483 void printList(LinkedList *list)
484 {
485 Node *node = list->head;
486 while (node != NULL)
487 {
488 printf("Node address: %p | ", node);
489 printf("data = %d| ", node->data);
490 printf("next node address = %p\n", node->next);
491 node = node->next;
492 }
493 printf("\n");
494 }
495
496 Node *deleteHead(LinkedList *list)
497 {
498 Node *node = list->head;
499 if (node == NULL)
500 {
501 printf("The linked list is empty.\n");

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 9/20
23:53 16/01/2024 test.c
502 return NULL;
503 }
504 else
505 {
506 list->head = node->next;
507 return node;
508 }
509 }
510
511 void deleteLinkedList(LinkedList *list)
512 {
513 while(list->head != NULL) {
514 Node *temp = deleteHead(list);
515 free(temp);
516 }
517 }
518
519 void insertNewNodeAtPositionK(LinkedList *list, int data, int k)
520 {
521 if (k <= 0 || k != 1 && list->head == NULL) {
522 printf("Error: Invalid position\n");
523 return;
524 }
525 if (k == 1) {
526 insertHead(data, list);
527 return;
528 }
529 Node *head_temp = list->head;
530 list->head = head_temp->next;
531 insertNewNodeAtPositionK(list, data, k - 1);
532 head_temp->next = list->head;
533 list->head = head_temp;
534 }
535
536 /*
537 Base Case: Khi k = 1, các trường hợp k <= 0, list rỗng là trường hợp input sai
538 Result BC: Thêm node mới vào đầu list bằng hàm insertHead
539 Before: k = 2, Bỏ đi node đầu tiên, insertHead list mới, thêm lại node đầu tiên
540 Before Before: k = 3, Bỏ đi node đầu tiên rồi tới node thứ 2, insertHead list mới, thêm
lại node thứ 2 vào đầu, thêm lại node đầu tiên vào đầu
541 */
542
543 Node *deletedeleteNodeAtPositionK(LinkedList *list, int k)
544 {
545 if (k <= 0 || list->head == NULL) {
546 printf("Error: Invalid position\n");
547 return NULL;
548 }
549 if (k == 1) {
550 return deleteHead(list);
551 }
552 Node *head_temp = list->head;
553 list->head = head_temp->next;
554 Node *res = deletedeleteNodeAtPositionK(list, k - 1);
555 head_temp->next = list->head;
556 list->head = head_temp;

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 10/20
23:53 16/01/2024 test.c
557 return res;
558 }
559
560 /*
561 Base Case: Khi k = 1, các trường hợp k <= 0, list rỗng là trường hợp input sai
562 Result BC: Xóa node đầu tiên trong list bằng hàm deleteHead
563 Before: k = 2, Bỏ đi node đầu tiên, deleteHead list mới, thêm lại node đầu tiên
564 Before Before: k = 3, Bỏ đi node đầu tiên rồi tới node thứ 2, deleteHead list mới, thêm
lại node thứ 2 vào đầu, thêm lại node đầu tiên vào đầu
565 */
566
567 int main()
568 {
569 LinkedList list;
570 init(&list);
571 insertNewNodeAtPositionK(&list, 2, 1);
572 insertNewNodeAtPositionK(&list, 3, 1);
573 insertNewNodeAtPositionK(&list, 5, 2);
574 insertNewNodeAtPositionK(&list, 4, 4);
575 printList(&list);
576 free(deletedeleteNodeAtPositionK(&list, 2));
577 printList(&list);
578 deleteLinkedList(&list);
579 return 0;
580 }
581
582
583 // BAI 5
584 // file hw05_partys.txt
585 // 10
586 // 12 A B S E F J K L S W Q A
587 // 10 T M K L J S A Q C S
588 // 9 H J S S A S M Q D
589 // 13 S A Q D A S S A Q D S Q A
590 // 15 Q S A Q E D F S S A S X V B F
591 // 14 H A V A N T H A O T T H U S
592 // 12 P H L A M D S A T T H U
593 // 15 P P N H U N G S V E L E V E N
594 // 10 L P T R U O N G D S
595 // 10 C H G I A O K D L H
596
597 // file hw05_room_state.txt
598 // 0 0 0 1 0 0 0 1 0 0 1 0
599 // 0 0 1 1 0 0 1 0 0 1 1 1
600 // 0 1 0 0 1 1 0 1 0 0 1 0
601 // 1 0 1 0 0 0 1 0 1 0 1 1
602 // 0 0 1 1 0 1 0 0 0 1 0 0
603 // 1 1 1 0 1 0 1 1 0 0 1 1
604 // 1 0 0 1 0 0 1 0 1 1 0 0
605 // 0 0 1 0 0 1 0 1 0 0 1 1
606 // 0 1 0 0 0 0 1 0 1 1 0 1
607 // 0 0 1 1 1 0 0 1 0 0 1 0
608 // 1 0 1 0 0 1 0 0 0 0 0 1
609 // 0 1 0 0 1 0 1 1 0 0 1 1
610 // 1 0 0 1 0 0 0 0 1 1 0 1
611 // 0 0 1 0 1 0 1 0 0 0 1 0

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 11/20
23:53 16/01/2024 test.c
612 // 1 0 0 1 0 1 0 1 1 0 0 1
613 // 1 0 1 0 0 0 1 0 1 0 1 0
614 // 0 0 0 0 0 0 0 0 0 0 1 1
615
616 // code
617
618 #include<stdio.h>
619 #include<stdlib.h>
620
621 #define _max_floor_ 17
622 #define _max_room_ 12
623
624 typedef struct RoomType{
625 char first_name;
626 int floor, room;
627 int state;
628 struct RoomType *next_room_on_floor;
629 struct RoomType *next_room_of_list;
630 } Room;
631
632 typedef struct FloorType{
633 Room *head;
634 struct FloorType *next;
635 } Floor;
636
637 typedef struct HotelType{
638 Floor *head;
639 } Hotel;
640
641 typedef struct ListRoomType{
642 Room *head;
643 } ListRoom;
644
645 void initFloor(Floor *room_list) {
646 room_list->head = NULL;
647 }
648
649 void initHotel(Hotel *hotel) {
650 hotel->head = NULL;
651 }
652
653 void initListRoom(ListRoom *list_rooms) {
654 list_rooms->head = NULL;
655 }
656 // Tạo danh sách liên kết chứa thông tin các phòng trong khách sạn
657 Room *makeRoom(int floor, int room, int state) {
658 Room *new_room = (Room*) malloc(sizeof(Room));
659 new_room->floor = floor;
660 new_room->room = room;
661 new_room->state = state;
662 new_room->next_room_on_floor = NULL;
663 new_room->next_room_of_list = NULL;
664 return new_room;
665 }
666 void insertRoomToFloor(Floor* room_list, int floor, int room, int state)
667 {

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 12/20
23:53 16/01/2024 test.c
668 Room *new_room = makeRoom(floor, room, state);
669 if (room_list->head == NULL) {
670 room_list->head = new_room;
671 }
672 else {
673 Room *temp = room_list->head;
674 while (temp->next_room_on_floor != NULL) {
675 temp = temp->next_room_on_floor;
676 }
677 temp->next_room_on_floor = new_room;
678 }
679 }
680
681 Floor *makeFloor(int floor, int state[])
682 {
683 Floor *new_floor = (Floor*) malloc(sizeof(Floor));
684 initFloor(new_floor);
685 for (int i = 0; i < _max_room_; i++) {
686 insertRoomToFloor(new_floor, floor, i, state[i]);
687 }
688 new_floor->next = NULL;
689 return new_floor;
690 }
691
692 void inserFloorToHotel(Hotel *hotel, int floor, int state[])
693 {
694 Floor *new_floor = makeFloor(floor, state);
695 if (hotel->head == NULL) {
696 hotel->head = new_floor;
697 }
698 else {
699 new_floor->next = hotel->head;
700 hotel->head = new_floor;
701 }
702 }
703
704 void createHotel(FILE *f, Hotel *hotel)
705 {
706 f = fopen("hw05_room_state.txt", "r");
707 if (!f) {
708 printf("File not found\n");
709 }
710 int state[_max_room_];
711 for (int i = _max_floor_ - 1; i >= 0; i--) {
712 for (int j = 0; j < _max_room_; j++) {
713 fscanf(f, "%d ", &state[j]);
714 }
715 inserFloorToHotel(hotel, i, state);
716 // for (int j = 0; j < _max_room_; j++) {
717 // printf("%d ", state[j]);
718 // }
719 // printf("\n");
720 }
721 fclose(f);
722 }
723

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 13/20
23:53 16/01/2024 test.c
724 void insertListRoom(ListRoom *list, Room *room) {
725 if (list->head == NULL) {
726 list->head = room;
727 }
728 else {
729 Room *temp = list->head;
730 while (temp->next_room_of_list != NULL) {
731 temp = temp->next_room_of_list;
732 }
733 temp->next_room_of_list = room;
734 }
735 }
736
737 void printHotel(Hotel *hotel) {
738 Floor* current_floor = hotel->head;
739 Room* current_room = NULL;
740 while (current_floor != NULL) {
741 current_room = current_floor->head;
742 while (current_room != NULL) {
743 printf("%d ", current_room->state);
744 current_room = current_room->next_room_on_floor;
745 }
746 printf("\n");
747 current_floor = current_floor->next;
748 }
749 }
750
751 void printListRoom(ListRoom *list_rooms) {
752 Room *current_room = list_rooms->head;
753 printf("First name\t| Current room\t| Next room\n");
754 while (current_room != NULL) {
755 printf("\t%c\t| ", current_room->first_name);
756 printf("P[%d].[%d]\t| ", current_room->floor, current_room->room);
757 if (current_room->next_room_of_list != NULL) {
758 printf("P[%d][%d]", current_room->next_room_of_list->floor, current_room->
next_room_of_list->room);
759 }
760 printf("\n");
761 current_room = current_room->next_room_of_list;
762 }
763 }
764
765 // Problem 1
766 int arrangePartysIntoEmptyRooms(FILE *f, Hotel *hotel);
767 Room *findEmptyRoomOnFloor(Hotel *hotel, int floor);
768
769 // Problem 2
770 Room *getRoomLeader(Hotel *hotel, int floor, int room);
771 void showInformationParty(Hotel *hotel);
772
773 // Problem 3
774 int checkOutSingleCustomer(Hotel *hotel, int floor, int room);
775 int addNewMemberIntoParty(Hotel *hotel, Room *leader_room, char first_name);
776 void additionalMember(Hotel *hotel);
777
778 // Problem 4

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 14/20
23:53 16/01/2024 test.c
779 Room* checkMemberOfParty(Hotel *hotel, int floor, int room, Room *leader_room);
780 void checkOutMemberOfParty(Hotel *hotel);
781
782 // Problem 5
783 void checkOutParty(Hotel *hotel);
784 void deleteListRoom(ListRoom *party);
785
786 void menu(Hotel *hotel);
787
788 int main()
789 {
790 FILE *fptr;
791 Hotel hotel;
792 initHotel(&hotel);
793
794 createHotel(fptr, &hotel);
795 arrangePartysIntoEmptyRooms(fptr, &hotel);
796
797 menu(&hotel);
798 return 0;
799 }
800
801
802 // Problem 1
803
804 int arrangePartysIntoEmptyRooms(FILE *f, Hotel *hotel)
805 {
806 // initListRoom(&empty_rooms);
807 // createListOfEmptyRooms(empty_rooms, hotel);
808 ListRoom party;
809 initListRoom(&party);
810
811 f = fopen("hw05_partys.txt", "r");
812 if (!f) {
813 printf("File not found!\n");
814 return 0;
815 }
816
817 int number_of_party;
818 fscanf(f, "%d", &number_of_party);
819
820 int number_of_member;
821 Room *temp_room;
822
823 for (int i = 0; i < number_of_party; i++) {
824 initListRoom(&party);
825 fscanf(f, "%d", &number_of_member);
826 char *list_name_member = (char*) malloc(number_of_member*sizeof(char));
827 for (int j = 0; j < number_of_member; j++) {
828 fscanf(f, " %c", &list_name_member[j]);
829 }
830
831 temp_room = findEmptyRoomOnFloor(hotel, 0);
832 if (temp_room == NULL) {
833 return 0;
834 }

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 15/20
23:53 16/01/2024 test.c
835 temp_room->next_room_of_list = NULL;
836 temp_room->state = 2;
837 temp_room->first_name = list_name_member[0];
838
839 insertListRoom(&party, temp_room);
840 for (int j = 1; j < number_of_member; j++) {
841 for (int k = 1; k < _max_floor_; k++) {
842 temp_room = findEmptyRoomOnFloor(hotel, k);
843 if (temp_room != NULL) {
844 break;
845 }
846 }
847 if (temp_room == NULL) {
848 return 0;
849 }
850 temp_room->next_room_of_list = NULL;
851 temp_room->state = 2;
852 temp_room->first_name = list_name_member[j];
853 insertListRoom(&party, temp_room);
854 }
855 free(list_name_member);
856 }
857 return 1;
858 }
859
860 Room *findEmptyRoomOnFloor(Hotel *hotel, int floor)
861 {
862 Floor *current_floor = hotel->head;
863 while (current_floor->head->floor != floor) {
864 current_floor = current_floor->next;
865 }
866 Room *current_room = current_floor->head;
867 while (current_room != NULL) {
868 if (current_room->state == 0) {
869 return current_room;
870 }
871 current_room = current_room->next_room_on_floor;
872 }
873 return NULL;
874 }
875
876 Room *getRoomLeader(Hotel *hotel, int floor, int room)
877 {
878 if (floor != 0 || room < 0 || room >= _max_room_) {
879 return NULL;
880 }
881 Room *current_room = hotel->head->head;
882 while (current_room != NULL) {
883 if (current_room->floor == floor && current_room->room == room && current_room->
state == 2) {
884 return current_room;
885 }
886 current_room = current_room->next_room_on_floor;
887 }
888 return NULL;
889 }

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 16/20
23:53 16/01/2024 test.c
890
891 void showInformationParty(Hotel *hotel)
892 {
893 int floor, room;
894 Room *temp_room;
895 ListRoom party;
896 initListRoom(&party);
897 do {
898 printf("Enter the floor and room number of the leader:\n");
899 scanf("\nP[%d].[%d]", &floor, &room);
900 temp_room = getRoomLeader(hotel, floor, room);
901 if (temp_room == NULL) {
902 printf("This isn't room number of any leader!\n");
903 }
904 } while (getRoomLeader(hotel, floor, room) == NULL);
905 party.head = temp_room;
906 printf("The party you want to find: \n");
907 printListRoom(&party);
908 }
909
910 int checkOutSingleCustomer(Hotel *hotel, int floor, int room)
911 {
912 if (floor < 0 || floor >= _max_floor_ || room < 0 || room >= _max_room_) {
913 return 0;
914 }
915 Floor *current_floor = hotel->head;
916 while (current_floor->head->floor != floor) {
917 current_floor = current_floor->next;
918 }
919 Room *current_room = current_floor->head;
920 while (current_room->room != room) {
921 current_room = current_room->next_room_on_floor;
922 }
923 if (current_room->state == 2 || current_room->state == 0) {
924 return 0;
925 }
926 else {
927 current_room->state = 0;
928 return 1;
929 }
930 }
931
932 int addNewMemberIntoParty(Hotel *hotel, Room *leader_room, char first_name)
933 {
934 Room *empty_room;
935 for (int i = 1; i < _max_floor_; i++) {
936 empty_room = findEmptyRoomOnFloor(hotel, i);
937 if (empty_room != NULL) {
938 break;
939 }
940 }
941 if (empty_room == NULL) {
942 return 0;
943 }
944 ListRoom party;
945 initListRoom(&party);

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 17/20
23:53 16/01/2024 test.c
946 party.head = leader_room;
947 empty_room->first_name = first_name;
948 empty_room->next_room_of_list = NULL;
949 empty_room->state = 2;
950 insertListRoom(&party, empty_room);
951 return 1;
952 }
953
954 void additionalMember(Hotel *hotel)
955 {
956 int floor, room;
957 do {
958 printf("Enter the number room of single customer want to check_out:\n");
959 scanf("\nP[%d].[%d]", &floor, &room);
960 if (!checkOutSingleCustomer(hotel, floor, room)) {
961 printf("This customer is not in our records.\nPlease try again.\n");
962 }
963 } while (!checkOutSingleCustomer(hotel, floor, room));
964 Room *leader_room;
965 do {
966 printf("Enter the floor and room number of the leader:\n");
967 scanf("\nP[%d].[%d]", &floor, &room);
968 leader_room = getRoomLeader(hotel, floor, room);
969 if (leader_room == NULL) {
970 printf("This isn't room number of any leader!\n");
971 }
972 } while (leader_room == NULL);
973 char first_name;
974 printf("Enter first character name member want to add:\n");
975 scanf("%c", &first_name);
976 if (addNewMemberIntoParty(hotel, leader_room, first_name)) {
977 printf("Added new member into party successfullly!\n");
978 }
979 else {
980 printf("Can't add this member into party. Party is full.\n");
981 }
982 }
983
984
985 Room* checkMemberOfParty(Hotel *hotel, int floor, int room, Room *leader_room) {
986 Room *temp_room = leader_room;
987 while (temp_room->next_room_of_list != NULL) {
988 if (temp_room->next_room_of_list->floor == floor && temp_room->next_room_of_list->
room == room) {
989 return temp_room;
990 }
991 temp_room = temp_room->next_room_of_list;
992 }
993 return NULL;
994 }
995
996 void checkOutMemberOfParty(Hotel *hotel)
997 {
998 int floor, room;
999 Room *leader_room;
1000 Room *member_room;

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 18/20
23:53 16/01/2024 test.c
1001 do {
1002 printf("Enter the number room of leader party have member want check_out:\n");
1003 scanf("\nP[%d].[%d]", &floor, &room);
1004 leader_room = getRoomLeader(hotel, floor, room);
1005 if (leader_room == NULL) {
1006 printf("There are no such a party.\nTry again please.\n");
1007 }
1008 } while (leader_room == NULL);
1009 do {
1010 printf("Enter the number room of member want check_out:\n");
1011 scanf("\nP[%d].[%d]", &floor, &room);
1012 member_room = checkMemberOfParty(hotel, floor, room, leader_room);
1013 if (member_room == NULL) {
1014 printf("There are no such a party.\nTry again please.\n");
1015 }
1016 } while (member_room == NULL);
1017 Room *deleted_room = member_room->next_room_of_list;
1018 member_room->next_room_of_list = deleted_room->next_room_of_list;
1019 deleted_room->state = 0;
1020 deleted_room->next_room_of_list = NULL;
1021 }
1022
1023 void checkOutParty(Hotel *hotel)
1024 {
1025 int floor, room;
1026 Room *leader_room;
1027 ListRoom party;
1028 do {
1029 printf("Enter the number room of leader party have member want check_out:\n");
1030 scanf("\nP[%d].[%d]", &floor, &room);
1031 leader_room = getRoomLeader(hotel, floor, room);
1032 if (leader_room == NULL) {
1033 printf("There are no such a party.\nTry again please.\n");
1034 }
1035 } while (leader_room == NULL);
1036 party.head = leader_room;
1037 deleteListRoom(&party);
1038 }
1039
1040 void deleteListRoom(ListRoom *party)
1041 {
1042 Room *temp_room = party->head;
1043 while (temp_room != NULL) {
1044 party->head = temp_room->next_room_of_list;
1045 temp_room->next_room_of_list = NULL;
1046 temp_room->state = 0;
1047 temp_room = party->head;
1048 }
1049 }
1050
1051 void menu(Hotel *hotel)
1052 {
1053 int i = 0;
1054 while (1) {
1055 system("cls");
1056 printf("\nMenu:\n");

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 19/20
23:53 16/01/2024 test.c
1057 printf("1. Confirm Information of Party.\n");
1058 printf("2. Check-out single customer and add new member to party.\n");
1059 printf("3. Check-out party member.\n");
1060 printf("4. Check-out party.\n");
1061 printf("5. Print state of hotel.\n");
1062 printf("6. Quit System.\n");
1063 printf("Input your choose: ");
1064 scanf("%d", &i);
1065 switch (i) {
1066 case 1:
1067 showInformationParty(hotel);
1068 system("pause");
1069 break;
1070 case 2:
1071 additionalMember(hotel);
1072 system("pause");
1073 break;
1074 case 3:
1075 checkOutMemberOfParty(hotel);
1076 system("pause");
1077 break;
1078 case 4:
1079 checkOutParty(hotel);
1080 system("pause");
1081 break;
1082 case 5:
1083 printHotel(hotel);
1084 system("pause");
1085 break;
1086 case 6:
1087 return;
1088 }
1089 }
1090 }

localhost:55497/8256552f-1dba-45cb-b84c-4db3e04ce59b/ 20/20

You might also like