Add cv4/a_train.c
This commit is contained in:
		
							parent
							
								
									732f87c434
								
							
						
					
					
						commit
						2d5d5625ed
					
				
							
								
								
									
										91
									
								
								cv4/a_train.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								cv4/a_train.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,91 @@ | ||||
| #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 = calloc(1, sizeof(struct car)); | ||||
| 
 | ||||
|     struct car* this ; | ||||
|     this = first; | ||||
| 
 | ||||
|     strcpy(newcar->value, target); | ||||
| 
 | ||||
|     if(first == NULL) | ||||
|     { | ||||
|         return newcar; | ||||
|     } | ||||
| 
 | ||||
|     while (this->next != NULL) | ||||
|     { | ||||
|        this = this->next; | ||||
|     } | ||||
|      | ||||
|     this->next = newcar; | ||||
|          | ||||
|     return first; | ||||
| } | ||||
| 
 | ||||
| void print_train(struct car* first) { | ||||
|     struct car* this ; | ||||
|     this = first; | ||||
| 
 | ||||
|     if (first == NULL) | ||||
|     { | ||||
|         return; | ||||
|     } | ||||
|     while (this != NULL) | ||||
|     { | ||||
|         printf("%s\n", this->value); | ||||
|         this = this->next; | ||||
|     } | ||||
|      | ||||
| } | ||||
| 
 | ||||
| void cancel_train(struct car* first) { | ||||
|     if (first == NULL) | ||||
|     { | ||||
|         return; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         cancel_train(first->next);     | ||||
|     } | ||||
|      | ||||
|     free(first); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| struct car* clear_train(struct car* first, const char* target) { | ||||
|     if (first == NULL) | ||||
|     { | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     while (first != NULL && strcmp(first->value, target) == 0) { | ||||
|         struct car* temp = first; // Зберігаємо вказівник на перший елемент
 | ||||
|         first = first->next;      // Переміщаємо вказівник на наступний елемент
 | ||||
|         free(temp);               // Вивільняємо пам'ять для видаленого елемента
 | ||||
|     } | ||||
| 
 | ||||
|     // Якщо список не порожній, продовжуємо перевіряти інші елементи
 | ||||
|     struct car* prev = first; // Вказівник на попередній елемент
 | ||||
|     struct car* this = first ? first->next : NULL; // Вказівник на поточний елемент
 | ||||
| 
 | ||||
|     while (this != NULL) { | ||||
|         if (strcmp(this->value, target) == 0) { | ||||
|             // Якщо значення елемента збігається з target
 | ||||
|             prev->next = this->next; // Перепідключаємо попередній елемент до наступного
 | ||||
|             free(this);              // Вивільняємо пам'ять для видаленого елемента
 | ||||
|             this = prev->next;       // Оновлюємо поточний елемент
 | ||||
|         } else { | ||||
|             // Якщо значення не збігається, рухаємося далі
 | ||||
|             prev = this;             // Оновлюємо попередній елемент
 | ||||
|             this = this->next;       // Рухаємося до наступного елемента
 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return first; // Повертаємо новий початок списку
 | ||||
| } | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user