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