From 8787edcb34b4d705452eb0eaa7286d8efea7f397 Mon Sep 17 00:00:00 2001 From: Maksym Malko Date: Fri, 6 Nov 2020 07:24:19 +0100 Subject: [PATCH] Five cviko --- cv5/a_train.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ cv5/a_train.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 cv5/a_train.c create mode 100644 cv5/a_train.h diff --git a/cv5/a_train.c b/cv5/a_train.c new file mode 100644 index 0000000..eb53ece --- /dev/null +++ b/cv5/a_train.c @@ -0,0 +1,54 @@ +#include "a_train.h" +#include + +struct car* add_car(struct car* first,const char* target) { + struct car *newCar = calloc(1,sizeof(struct car)); + strcpy(newCar->value,target); + + if(first==NULL){ + first = newCar; + }else{ + struct car* this = first; + while(this->next!=NULL){ + this = this->next; + } + this->next = newCar; + } + return first; +} + +void print_train(struct car* first) { + for(struct car* this = first;this!=NULL;this = this->next){ + printf("%s\n",this->value); + } +} + +void cancel_train(struct car* first){ + if(first==NULL){ + return; + } + cancel_train(first->next); + free(first); +} + + +struct car* clear_train(struct car* first, const char* target) { + + struct car *previous = NULL; + for(struct car *this = first;this!=NULL;this=this->next){ + if(strcmp(this->value,target)==0){ + if(previous==NULL){ + first = first->next; + free(this); + break; + }else{ + previous->next = this->next; + free(this); + break; + } + + } + previous = this; + } + return first; +} diff --git a/cv5/a_train.h b/cv5/a_train.h new file mode 100644 index 0000000..70e7409 --- /dev/null +++ b/cv5/a_train.h @@ -0,0 +1,51 @@ +#ifndef TRAIN_H +#define TRAIN_H +#define SIZE 100 + +/** + * Jeden vozen vlaku + */ +struct car { + /** + * Nazov cielovej stanice + */ + char value[SIZE]; + /** + * Smenik na dalsi vozen + */ + struct car* next; +}; + +/** + * Prida vozen na koniec vlaku. + * + * @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu. + * @return smernik na zaciatok vlaku. + */ +struct car* add_car(struct car* first,const char* target); + + +/** + * Vypise vsetky vozne vo vlaku + * + * @arg smernik na prvy vozen + */ +void print_train(struct car* first); + +/** + * Zrusenie vsetkych voznov vo vlaku. + * @arg smernik na prvy vozen + */ +void cancel_train(struct car* first); + +/** + * Vyradenie vsetkych voznov, ktorych cielova stanica je target + * + * @arg smernik na prvy vozen + * @arg cielova stanica, ktora sa ma vyradit z vlaku. + * @return smernik na novy prvy vozen + * + */ +struct car* clear_train(struct car* first,const char* target); + +#endif // TRAIN_H \ No newline at end of file