From 2e2f84f08c8767aa6fcb586cbbc6c8d0757074de Mon Sep 17 00:00:00 2001 From: Yuliia Pikiner Date: Fri, 6 Nov 2020 20:30:26 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20'cv6'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv6/Makefile | 13 +++++ cv6/a_train.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++ cv6/a_train.h | 51 +++++++++++++++++ cv6/main.c | 29 ++++++++++ 4 files changed, 242 insertions(+) create mode 100644 cv6/Makefile create mode 100644 cv6/a_train.c create mode 100644 cv6/a_train.h create mode 100644 cv6/main.c diff --git a/cv6/Makefile b/cv6/Makefile new file mode 100644 index 0000000..3c2c463 --- /dev/null +++ b/cv6/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/cv6/a_train.c b/cv6/a_train.c new file mode 100644 index 0000000..35d3726 --- /dev/null +++ b/cv6/a_train.c @@ -0,0 +1,149 @@ +#include "a_train.h" +#include +#include +#include + +int getLength(struct car* first) { + + if (first != NULL) { + + int count = 1; + + while (first->next != NULL) { + + count++; + first = first->next; + + } + + return count; + + } else { + + return 0; + + } + +} + +struct car* getLast(struct car* first) { + + if (first == NULL) return NULL; + + while (first->next != NULL) first = first->next; + + return first; + +} + +struct car* getLastButOne(struct car* first) { + + if (first == NULL || first->next == NULL) return NULL; + + while (first->next->next != NULL) first = first->next; + + return first; + +} + +struct car* add_car(struct car* first, const char* target) { + + struct car* newcar = calloc(1,sizeof(struct car)); + + strcpy(newcar->value, target); + newcar->next = NULL; + + if (first == NULL) return newcar; + + struct car* last = getLast(first); + + last->next = newcar; + + return first; + +} + +void print_train(struct car* first) { + + if (first == NULL) return; + + struct car* this = first; + + printf("\nTrain way:\n\n"); + printf("%s", this->value); + + while (this->next != NULL) { + + this = this->next; + printf(" -> "); + printf("%s", this->value); + + } + + printf("\n\n"); + +} + +void popBack(struct car* first) { + + if (first == NULL) { printf("123"); return; } + + struct car* lastbo = getLastButOne(first); + + if (lastbo == NULL) { + + free(first); + first = NULL; + + } else { + + free(lastbo->next); + lastbo->next = NULL; + + } + +} + +void cancel_train(struct car* first) { + + if (first == NULL) return; + + while (first->next != NULL) popBack(first); + + free(first); + first = NULL; + +} + +struct car* clear_train(struct car* first, const char* target) { + + if (first == NULL) return NULL; + + // while (first->next->next != NULL) { + + // if (strcmp(first->next->value, target) == 0) { + + // struct car* node = first->next->next; + + // free(first->next); + + // first->next = node; + + // } + + // } + + struct car* this = first; + + if (strcmp(first->value, target) == 0) { + + this = this->next; + + free(first); + + } + + return this; + +} + diff --git a/cv6/a_train.h b/cv6/a_train.h new file mode 100644 index 0000000..ed0863f --- /dev/null +++ b/cv6/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/cv6/main.c b/cv6/main.c new file mode 100644 index 0000000..38352a5 --- /dev/null +++ b/cv6/main.c @@ -0,0 +1,29 @@ +#include "a_train.h" +#include +#include + +// Testovaci subor pre vlak +int main(void){ + + struct car* train = NULL; + + train = add_car(train, "Presov"); + train = add_car(train, "Presov"); + train = add_car(train, "Presov"); + train = add_car(train, "Presov"); + train = add_car(train, "Presov"); + 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); + + train = clear_train(train, "Presov"); + print_train(train); + + // cancel_train(train); + // print_train(train); + + return 0; + +}