From 9a7f29797fdd45972acb4d46de85d70744b7bbea Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 12:34:20 +0000 Subject: [PATCH 01/11] Initializacia --- cv4/Makefile | 13 +++++++++++++ cv4/a_train.c | 18 ++++++++++++++++++ cv4/a_train.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ cv4/main.c | 17 +++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 cv4/Makefile create mode 100644 cv4/a_train.c create mode 100644 cv4/a_train.h create mode 100644 cv4/main.c diff --git a/cv4/Makefile b/cv4/Makefile new file mode 100644 index 0000000..3c2c463 --- /dev/null +++ b/cv4/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/cv4/a_train.c b/cv4/a_train.c new file mode 100644 index 0000000..3ecfbc8 --- /dev/null +++ b/cv4/a_train.c @@ -0,0 +1,18 @@ +#include "a_train.h" +#include + +struct car* add_car(struct car* first,const char* target) { + return NULL; +} + +void print_train(struct car* first) { +} + +void cancel_train(struct car* first) { +} + + +struct car* clear_train(struct car* first, const char* target) { + return NULL; +} + diff --git a/cv4/a_train.h b/cv4/a_train.h new file mode 100644 index 0000000..5982bfe --- /dev/null +++ b/cv4/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/cv4/main.c b/cv4/main.c new file mode 100644 index 0000000..d64f05a --- /dev/null +++ b/cv4/main.c @@ -0,0 +1,17 @@ +#include "a_train.h" +#include + +// Testovaci subor pre vlak +int main(){ + 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); + cancel_train(train); + + return 0; +} From 5e43f512cfb0106578fc32abbfbb0d2a7d4f6870 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 12:36:12 +0000 Subject: [PATCH 02/11] Initializacia --- cv4/program.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 cv4/program.c diff --git a/cv4/program.c b/cv4/program.c deleted file mode 100644 index e69de29..0000000 From e84146551bc51b65256b4b68b2fe414294696f6f Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 13:43:00 +0000 Subject: [PATCH 03/11] Initializacia --- cv4/a_train.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 3ecfbc8..edd3bfb 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -1,18 +1,67 @@ #include "a_train.h" #include +#include +#include struct car* add_car(struct car* first,const char* target) { - return NULL; + struct car* last = first; + struct car* newcar* = calloc(1, sizeof(struct car)); + strcpy(newcar->value, target); + if (last != NULL){ + struct car* current = last; + while(current->next != NULL){ + current = current->next + } + current->next = newcar; + } else{ + last = newcar; + return last; + } + return first; } void print_train(struct car* first) { + if(first != NULL){ + struct car* current = first; + while(current->next != NULL){ + printf("%s\n", current->value); + current = current->next; + } + } } void cancel_train(struct car* first) { + struct car* current = first; + while(first != NULL){ + current = current->next; + free(first); + first = current; + } } struct car* clear_train(struct car* first, const char* target) { - return NULL; + if (first = NULL){ + return NULL; + } + struct car* current = first, new_first = first; + if (first != NULL){ + if(strcmp(first->destination, target) == 0){ + new_first = new_first->next; + free(first); + first = new_first; + } + } + + while (first->next != NULL) { + current = first->next; + if (strcmp(current->destination, target) == 0) { + first->next = current->next; + free(current); + } else { + first = current; + } + } + return new_first; } From 58777d185006b18282bdde1200c82275297aa534 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 13:46:29 +0000 Subject: [PATCH 04/11] Initializacia --- cv4/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv4/main.c b/cv4/main.c index d64f05a..8c6a5c4 100644 --- a/cv4/main.c +++ b/cv4/main.c @@ -9,7 +9,7 @@ int main(){ train = add_car(train,"Levoca"); train = add_car(train,"Spiska Nova Ves"); print_train(train); - clear_train(train,"Levoca"); + tran = clear_train(train,"Levoca"); print_train(train); cancel_train(train); From df2f224230a1d35f302b9ef0ee49c5215b06e2dd Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 13:46:47 +0000 Subject: [PATCH 05/11] Initializacia --- cv4/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv4/main.c b/cv4/main.c index 8c6a5c4..a445b0d 100644 --- a/cv4/main.c +++ b/cv4/main.c @@ -9,7 +9,7 @@ int main(){ train = add_car(train,"Levoca"); train = add_car(train,"Spiska Nova Ves"); print_train(train); - tran = clear_train(train,"Levoca"); + train = clear_train(train,"Levoca"); print_train(train); cancel_train(train); From 6448cb0e4df871ad932053fa51b7044c1b1ac2ce Mon Sep 17 00:00:00 2001 From: Kozar Date: Sat, 19 Oct 2024 11:47:21 +0000 Subject: [PATCH 06/11] Initializacia --- cv4/a_train.c | 68 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index edd3bfb..4be60ce 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -21,47 +21,47 @@ struct car* add_car(struct car* first,const char* target) { } void print_train(struct car* first) { - if(first != NULL){ - struct car* current = first; - while(current->next != NULL){ - printf("%s\n", current->value); - current = current->next; - } - } + // if(first != NULL){ + // struct car* current = first; + // while(current->next != NULL){ + // printf("%s\n", current->value); + // current = current->next; + // } + // } } void cancel_train(struct car* first) { - struct car* current = first; - while(first != NULL){ - current = current->next; - free(first); - first = current; - } + // struct car* current = first; + // while(first != NULL){ + // current = current->next; + // free(first); + // first = current; + // } } struct car* clear_train(struct car* first, const char* target) { - if (first = NULL){ - return NULL; - } - struct car* current = first, new_first = first; - if (first != NULL){ - if(strcmp(first->destination, target) == 0){ - new_first = new_first->next; - free(first); - first = new_first; - } - } + // if (first = NULL){ + // return NULL; + // } + // struct car* current = first, new_first = first; + // if (first != NULL){ + // if(strcmp(first->destination, target) == 0){ + // new_first = new_first->next; + // free(first); + // first = new_first; + // } + // } - while (first->next != NULL) { - current = first->next; - if (strcmp(current->destination, target) == 0) { - first->next = current->next; - free(current); - } else { - first = current; - } - } - return new_first; + // while (first->next != NULL) { + // current = first->next; + // if (strcmp(current->destination, target) == 0) { + // first->next = current->next; + // free(current); + // } else { + // first = current; + // } + // } + // return new_first; } From 2e25765e5d90fe45a38db591666461517fab628e Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 20 Oct 2024 12:58:05 +0000 Subject: [PATCH 07/11] Initializacia --- cv4/a_train.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 4be60ce..f881ba2 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -3,20 +3,22 @@ #include #include -struct car* add_car(struct car* first,const char* target) { +struct car* add_car(struct car* first, const char* target) { struct car* last = first; - struct car* newcar* = calloc(1, sizeof(struct car)); + struct car* newcar = calloc(1, sizeof(struct car)); strcpy(newcar->value, target); - if (last != NULL){ + + if (last != NULL) { struct car* current = last; - while(current->next != NULL){ - current = current->next + while (current->next != NULL) { + current = current->next; } current->next = newcar; - } else{ + } else { last = newcar; return last; } + return first; } From 9cd93271e18936196462d93369f3f2fa3dd1f17c Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 20 Oct 2024 12:59:11 +0000 Subject: [PATCH 08/11] Initializacia --- cv4/a_train.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index f881ba2..021890c 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -23,13 +23,13 @@ struct car* add_car(struct car* first, const char* target) { } void print_train(struct car* first) { - // if(first != NULL){ - // struct car* current = first; - // while(current->next != NULL){ - // printf("%s\n", current->value); - // current = current->next; - // } - // } + if(first != NULL){ + struct car* current = first; + while(current->next != NULL){ + printf("%s\n", current->value); + current = current->next; + } + } } void cancel_train(struct car* first) { From 6c79f0d25092133328c30cd8d77ab0a3527b322d Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 20 Oct 2024 13:00:06 +0000 Subject: [PATCH 09/11] Initializacia --- cv4/a_train.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 021890c..b175afa 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -43,27 +43,27 @@ void cancel_train(struct car* first) { struct car* clear_train(struct car* first, const char* target) { - // if (first = NULL){ - // return NULL; - // } - // struct car* current = first, new_first = first; - // if (first != NULL){ - // if(strcmp(first->destination, target) == 0){ - // new_first = new_first->next; - // free(first); - // first = new_first; - // } - // } + if (first = NULL){ + return NULL; + } + struct car* current = first, new_first = first; + if (first != NULL){ + if(strcmp(first->destination, target) == 0){ + new_first = new_first->next; + free(first); + first = new_first; + } + } - // while (first->next != NULL) { - // current = first->next; - // if (strcmp(current->destination, target) == 0) { - // first->next = current->next; - // free(current); - // } else { - // first = current; - // } - // } - // return new_first; + while (first->next != NULL) { + current = first->next; + if (strcmp(current->destination, target) == 0) { + first->next = current->next; + free(current); + } else { + first = current; + } + } + return new_first; } From 91662d4ed85c3c0aa06b94e9d7b297fb412a5f45 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 20 Oct 2024 13:02:50 +0000 Subject: [PATCH 10/11] Initializacia --- cv4/a_train.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index b175afa..5e87327 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -48,7 +48,7 @@ struct car* clear_train(struct car* first, const char* target) { } struct car* current = first, new_first = first; if (first != NULL){ - if(strcmp(first->destination, target) == 0){ + if(strcmp(first->value, target) == 0){ new_first = new_first->next; free(first); first = new_first; @@ -57,7 +57,7 @@ struct car* clear_train(struct car* first, const char* target) { while (first->next != NULL) { current = first->next; - if (strcmp(current->destination, target) == 0) { + if (strcmp(current->value, target) == 0) { first->next = current->next; free(current); } else { From c79715105ca63b19c3b9de21c4c43db00e8fa9df Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 20 Oct 2024 13:06:03 +0000 Subject: [PATCH 11/11] Initializacia --- cv4/a_train.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 5e87327..c117b1a 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -33,37 +33,40 @@ void print_train(struct car* first) { } void cancel_train(struct car* first) { - // struct car* current = first; - // while(first != NULL){ - // current = current->next; - // free(first); - // first = current; - // } + struct car* current = first; + while(first != NULL){ + current = current->next; + free(first); + first = current; + } } struct car* clear_train(struct car* first, const char* target) { - if (first = NULL){ + if (first == NULL) { return NULL; } - struct car* current = first, new_first = first; - if (first != NULL){ - if(strcmp(first->value, target) == 0){ - new_first = new_first->next; - free(first); - first = new_first; - } + + struct car* current = first; + struct car* previous = NULL; + + while (current != NULL && strcmp(current->value, target) == 0) { + struct car* temp = current; + first = current->next; + current = first; + free(temp); } - while (first->next != NULL) { - current = first->next; + while (current != NULL) { if (strcmp(current->value, target) == 0) { - first->next = current->next; + previous->next = current->next; free(current); + current = previous->next; } else { - first = current; + previous = current; + current = current->next; } } - return new_first; -} + return first; +}