#include "a_train.h" #include #include #include struct car* add_car(struct car* first,const char* target) { struct car* newcar=calloc(1, sizeof(struct car)); strcpy(newcar->value, target); newcar->next=NULL; if (first==NULL) { return newcar; } struct car* this=first; while (this->next!=NULL) { this=this->next; } this->next=newcar; return first; return NULL; } void print_train(struct car* first) { struct car* current=first; while (current!=NULL) { printf("%s\n", current->value); current=current->next; } } void cancel_train(struct car* first) { struct car* current=first; while (current!=NULL) { struct car* next=current->next; free(current); current=next; } } struct car* clear_train(struct car* first, const char* target) { while (first!=NULL && strcmp(first->value, target)==0) { struct car* temp=first; first=first->next; free(temp); } if (first==NULL) { return NULL; } struct car* current=first; while (current->next!=NULL) { if (strcmp(current->next->value, target)==0) { struct car* temp=current->next; current->next=current->next->next; free(temp); } else { current=current->next; } } return first; }