diff --git a/du4/Makefile b/du4/Makefile new file mode 100644 index 0000000..3c2c463 --- /dev/null +++ b/du4/Makefile @@ -0,0 +1,13 @@ +CFLAGS= -std=c99 -g -Wall + +all: train + +%.o: %.c + gcc -c -o $@ $< $(CFLAGS) + +train: main.o a_train.o + gcc main.o a_train.o -o train + +clean: + rm *.o train + diff --git a/du4/a_train.c b/du4/a_train.c new file mode 100644 index 0000000..37e95fd --- /dev/null +++ b/du4/a_train.c @@ -0,0 +1,74 @@ +#include "a_train.h" +#include +#include +#include + +struct car* add_car(struct car* first,const char* target) { + if(first == NULL){ + first = calloc(1, sizeof(struct car)); + strcpy(first->value, target); + return first; + } + + struct car* temp = first; + while(temp->next){ + temp = temp->next; + } + struct car* newCar = (struct car*)calloc(1, sizeof(struct car)); + strcpy(newCar->value, target); + temp->next = newCar; + return first; + +} + +void print_train(struct car* first) { + struct car* temp = first; + while(temp){ + printf("%s\n", temp->value); + temp = temp->next; + + } + + +} + +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) { + if(first == NULL){ + return NULL; + } + if (first->next == NULL && strcmp(first->value, target) == 0) + { + free(first); + return NULL; + } + else if(first->next == NULL && strcmp(first->value, target) != 0){ + return first; + } + struct car* temp = first; + struct car* next = first->next; + while (next->next) + {if(strcmp(next->value, target) == 0){ + + temp->next = next->next; + free(next); + next = temp->next; + + } + else{ + temp = next; + next = next->next;} + + } + + return first; +} + diff --git a/du4/a_train.h b/du4/a_train.h new file mode 100644 index 0000000..5982bfe --- /dev/null +++ b/du4/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 diff --git a/du4/a_train.o b/du4/a_train.o new file mode 100644 index 0000000..28cfb76 Binary files /dev/null and b/du4/a_train.o differ diff --git a/du4/input.txt b/du4/input.txt new file mode 100644 index 0000000..7128fcc --- /dev/null +++ b/du4/input.txt @@ -0,0 +1 @@ +Bratislava diff --git a/du4/main.c b/du4/main.c new file mode 100644 index 0000000..9f43a8f --- /dev/null +++ b/du4/main.c @@ -0,0 +1,35 @@ +#include "a_train.h" +#include +#include + +// Testovaci subor pre vlak +int main(){ + char buff[1000]; + struct car* train = NULL; + train = add_car(train,"Presov"); + train = add_car(train,"Bratislava"); + train = add_car(train,"Levoca"); + train = add_car(train,"Spiska Nova Ves"); + print_train(train); + clear_train(train,"Levoca"); + print_train(train); + printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n"); + printf("Zoznam zakončite prázdnym riadkom.\n"); + while (fgets(buff, SIZE, stdin)) + { + add_car(train, buff); + + } + print_train(train); + printf("Zadajte stanicu, ktorá sa má vyradiť:\n"); + char buff2[SIZE]; + fgets(buff2, SIZE, stdin); + printf("%s\n", buff2); + printf("Výsledný vlak bez stanice %s bude:\n", buff2); + clear_train(train, buff2); + print_train(train); + + + + return 0; +} diff --git a/du4/main.o b/du4/main.o new file mode 100644 index 0000000..7032b29 Binary files /dev/null and b/du4/main.o differ diff --git a/du4/train b/du4/train new file mode 100755 index 0000000..91dcb39 Binary files /dev/null and b/du4/train differ