Update cv4/a_train.c

This commit is contained in:
Yurii Yakovenko 2024-10-24 21:02:16 +00:00
parent 76864bd125
commit 8897fa16b1

View File

@ -3,6 +3,11 @@
struct car
{
char value[SIZE]; // Cieľová stanica
struct car* next; // Ďaľší záznam, môže byť NULL
};
struct car* add_car(struct car* first,const char* target) struct car* add_car(struct car* first,const char* target)
{ {
@ -30,6 +35,7 @@ void print_train(struct car* first)
{ {
if(first==NULL) if(first==NULL)
{ {
return; return;
} }
@ -37,9 +43,7 @@ void print_train(struct car* first)
{ {
printf("%s", first->value); printf("%s", first->value);
first=first->next; first=first->next;
if(first){ if(first) printf("->");
printf("->");
}
}while(first!=NULL); }while(first!=NULL);
} }
@ -57,43 +61,45 @@ void cancel_train(struct car* first)
struct car* clear_train(struct car* first, const char* target) struct car* clear_train(struct car* first, const char* target)
{ {
struct car* t; //змінна для збереження кого видаляти struct car* t;
struct car* ptr;
if(first==NULL || target==NULL) if(first==NULL || target==NULL)
{ {
return first; return first;
} }
//голова == таргет - видаляємо лише перший while(strcmp(first->value, target)==0)
while(strcmp(first->value, target)==0) //поки рядки однакові
{ {
t=first; t=first;
first=first->next; first=first->next;
free(t); free(t);
if(first==NULL) if(first==NULL)
{ {
return NULL; return NULL;
} }
} }
//голова не таргет
ptr=first;
do do
{ {
if(strcmp(first->next->value, target)==0) if(strcmp(ptr->next->value, target)==0)
{ {
t=first; t=ptr->next;;
first=first->next; ptr->next = ptr -> next->next;
free(t); free(t);
} }
else else
{ {
first=first->next; ptr = ptr -> next;
} }
} }while(ptr->next);
while(first);
return first; return first;
} }