diff --git a/du4/Makefile b/du4/Makefile new file mode 100644 index 0000000..ad8bfaa --- /dev/null +++ b/du4/Makefile @@ -0,0 +1,24 @@ +TARGET = train + +CC = gcc + +CFLAGS = -Wall -Wextra -std=c11 -g + +OBJS = main.o a_train.o + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) + +main.o: main.c a_train.h + $(CC) $(CFLAGS) -c main.c + +a_train.o: a_train.c a_train.h + $(CC) $(CFLAGS) -c a_train.c + +clean: + rm -f $(OBJS) $(TARGET) + +run: $(TARGET) + ./$(TARGET) diff --git a/du4/a_train.c b/du4/a_train.c new file mode 100644 index 0000000..d8632bd --- /dev/null +++ b/du4/a_train.c @@ -0,0 +1,79 @@ +#include "a_train.h" +#include +#include +#include + +//pridaj vozen na koniec spojkového zoznamu +struct car* add_car(struct car* first, const char* target) { + // alokacia noveho prvku + struct car* newcar = calloc(1, sizeof(struct car)); + if (newcar == NULL) { + fprintf(stderr, "Chyba: nepodarilo sa alokovať pamäť pre nový vozeň.\n"); + return first; + } + + // kopirujeme cielovu stanicu + strcpy(newcar->value, target); + newcar->next = NULL; + + // vlak prazdny -> novy vozen je prvy + if (first == NULL) { + return newcar; + } + + // inak pridame na koniec + struct car* this = first; + while (this->next != NULL) { + this = this->next; + } + this->next = newcar; + + return first; +} + +// vypis voznov +void print_train(struct car* first) { + struct car* this = first; + while (this != NULL) { + printf("%s\n", this->value); + this = this->next; + } +} + +// zrusit vlak a uvolnit pamat +void cancel_train(struct car* first) { + if (first == NULL) return; + + // Rekurzívne uvoľnenie + cancel_train(first->next); + free(first); +} + +// odstranit vozne s target stanicou +struct car* clear_train(struct car* first, const char* target) { + struct car* current = first; + struct car* prev = NULL; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + // vymazat zaznam + struct car* to_delete = current; + current = current->next; + + if (prev == NULL) { + // vymazat prvy prvok + first = current; + } else { + prev->next = current; + } + + free(to_delete); + } else { + // Pokračujeme ďalej + prev = current; + current = current->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/main.c b/du4/main.c new file mode 100644 index 0000000..8a17f1c --- /dev/null +++ b/du4/main.c @@ -0,0 +1,30 @@ +#include "a_train.h" +#include +#include +#include + +int main() { + struct car* train = NULL; + char name[SIZE]; + + printf("Zadajte zoznam cieľových staníc (ukončite prázdnym riadkom):\n"); + while (1) { + if (fgets(name, SIZE, stdin) == NULL) break; + // odstranit '\n' + name[strcspn(name, "\n")] = '\0'; + if (strlen(name) == 0) break; + train = add_car(train, name); + } + + printf("\nZadajte stanicu, ktorá sa má vyradiť:\n"); + fgets(name, SIZE, stdin); + name[strcspn(name, "\n")] = '\0'; + + train = clear_train(train, name); + + printf("\nVýsledný vlak bez stanice %s bude:\n", name); + print_train(train); + + cancel_train(train); + return 0; +}