Add cv4/a_train.c
This commit is contained in:
		
							parent
							
								
									768000285e
								
							
						
					
					
						commit
						5d54025349
					
				
							
								
								
									
										80
									
								
								cv4/a_train.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								cv4/a_train.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | ||||
| #include "a_train.h" | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| 
 | ||||
| struct car* add_car(struct car* first, const char* target) { | ||||
|     struct car* newcar = (struct car*)calloc(1, sizeof(struct car)); | ||||
|     if (newcar == NULL) { | ||||
|         printf("Ошибка: не удалось выделить память.\n"); | ||||
|         exit(1); | ||||
|     } | ||||
| 
 | ||||
|     strcpy(newcar->value, target); | ||||
|     newcar->next = NULL; | ||||
| 
 | ||||
|     if (first == NULL) { | ||||
|         return newcar; | ||||
|     } | ||||
| 
 | ||||
|     struct car* current = first; | ||||
|     while (current->next != NULL) { | ||||
|         current = current->next; | ||||
|     } | ||||
| 
 | ||||
|     current->next = newcar; | ||||
|     return first; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void print_train(struct car* first) { | ||||
|     struct car* current = first; | ||||
| 
 | ||||
|     if (current == NULL) { | ||||
|         printf("Поезд пуст.\n"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     while (current != NULL) { | ||||
|         printf("%s\n", current->value); | ||||
|         current = current->next; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void cancel_train(struct car* first) { | ||||
|     if (first == NULL) { | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     cancel_train(first->next); | ||||
|     free(first); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| struct car* clear_train(struct car* first, const char* target) { | ||||
|     struct car* prev = NULL; | ||||
|     struct car* current = first; | ||||
| 
 | ||||
|     while (current != NULL) { | ||||
|         if (strcmp(current->value, target) == 0) { | ||||
|             if (prev == NULL) { | ||||
|                 struct car* temp = current->next; | ||||
|                 free(current); | ||||
|                 current = temp; | ||||
|                 first = current; | ||||
|             } else { | ||||
|                 prev->next = current->next; | ||||
|                 free(current); | ||||
|                 current = prev->next; | ||||
|             } | ||||
|         } else { | ||||
|             prev = current; | ||||
|             current = current->next; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return first; | ||||
| } | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user