From a20c34c593d4380c615815c3592e48d06ac65a91 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Tue, 9 Nov 2021 14:16:25 +0100 Subject: [PATCH] add --- a3/a3.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ cv4/a_train.c | 67 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 a3/a3.c diff --git a/a3/a3.c b/a3/a3.c new file mode 100644 index 0000000..5e4fe64 --- /dev/null +++ b/a3/a3.c @@ -0,0 +1,71 @@ +#include +#include + +#define SIZE 8 + +struct pizza{ + int price; + char name[100]; +}; + +void insertSort(struct pizza array[SIZE]){ + struct pizza temp; + for(int i = 1; i < SIZE; i++){ + if(array[i].price < array[i-1].price){ + for(int j = i-1; j >= 0; j--){ + if(array[i].price > array[j].price){ + break; + } + else if(array[i].price == array[j].price){ + if(strcmp(array[i].name, array[j].name) < 0){ + temp = array[j]; + array[j] = array[i]; + array[i] = temp; + i = j; + } + } + else{ + temp = array[j]; + array[j] = array[i]; + array[i] = temp; + i = j; + } + } + } + } +} + +int main(){ + struct pizza array[SIZE]; + + array[0].price = 5; + strcpy(array[0].name, "Cecko Pizza"); + + array[1].price = 1; + strcpy(array[1].name, "Hacko Pizza"); + + array[2].price = 10; + strcpy(array[2].name, "Acko Pizza"); + + array[3].price = 5; + strcpy(array[3].name, "Acko Pizza"); + + array[4].price = 5; + strcpy(array[4].name, "Becko Pizza"); + + array[5].price = 5; + strcpy(array[5].name, "Aacko Pizza"); + + array[6].price = 8; + strcpy(array[6].name, "Becko Pizza"); + + array[7].price = 0; + strcpy(array[7].name, "Becko Pizza"); + + insertSort(array); + + for(int i = 0; i < SIZE; i++) + printf("%d %s\n", array[i].price, array[i].name); + + return 0; +} \ No newline at end of file diff --git a/cv4/a_train.c b/cv4/a_train.c index 3ecfbc8..f456201 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -1,18 +1,69 @@ -#include "a_train.h" #include +#include +#include +#include "a_train.h" +struct car* add_car(struct car* first, const char* target){ + if(!strcmp(target, "")) + return NULL; -struct car* add_car(struct car* first,const char* target) { - return NULL; + if(!first){ + first = (struct car*)calloc(1, sizeof(struct car)); + strcpy(first->value, target); + first->next = NULL; + return first; + } + + struct car* temp = first; + + while(temp->next != NULL) + temp = temp->next; + + temp->next = (struct car*)calloc(1, sizeof(struct car)); + strcpy(temp->next->value, target); + temp->next->next = NULL; + return first; } -void print_train(struct car* first) { +void print_train(struct car* first){ + struct car* temp = first; + + while(temp != NULL){ + printf("%s\n", temp->value); + temp = temp->next; + } } -void cancel_train(struct car* first) { +void cancel_train(struct car* first){ + if(!first) + return; + + struct car* temp = first; + struct car* next = temp->next; + + while(next != NULL){ + next = temp->next; + free(temp); + temp = next; + } + + first = NULL; } +struct car* clear_train(struct car* first, const char* target){ + if(!first || !strcmp(target, "")) + return NULL; -struct car* clear_train(struct car* first, const char* target) { - return NULL; -} + struct car* temp = first; + struct car* next = temp->next; + while(temp != NULL){ + next = temp->next; + if(!strcmp(temp->value, target)) + free(temp); + temp = next; + if(!first) + first = temp; + } + + return first; +} \ No newline at end of file