diff --git a/a_train.c b/a_train.c index 3ecfbc8..50a39c2 100644 --- a/a_train.c +++ b/a_train.c @@ -1,18 +1,95 @@ #include "a_train.h" #include +#include +#include struct car* add_car(struct car* first,const char* target) { - return NULL; + 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) { - return NULL; + 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; } diff --git a/main.c b/main.c index d64f05a..9f86a30 100644 --- a/main.c +++ b/main.c @@ -1,17 +1,37 @@ #include "a_train.h" #include +#include +#include // Testovaci subor pre vlak int main(){ + struct car* train = NULL; + /* int c=0; + printf("Zadajte nazov stanice a pocet cestujucich. \n Ukoncite prazdnym riadkom \n"); + + + char i[100]; + while (strcmp(i, "\n") != 0) { + c++; + fgets(i, 100, stdin); + + if(c%2 != 0){ + train = add_car(train,i); + } + printf("%s \n",i); + + + + }*/ + //struct car* train = NULL; train = add_car(train,"Presov"); - train = add_car(train,"Bratislava"); train = add_car(train,"Levoca"); + train = add_car(train,"Bratislava"); train = add_car(train,"Spiska Nova Ves"); print_train(train); clear_train(train,"Levoca"); print_train(train); cancel_train(train); - return 0; }