From 537edea3d5954fc6c41b4c1d687c5f3ecc8dad47 Mon Sep 17 00:00:00 2001 From: Nikita Sazonov Date: Fri, 20 Aug 2021 15:20:47 +0300 Subject: [PATCH 01/23] Add c99 flag --- courses/cunix/ex01/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/courses/cunix/ex01/Makefile b/courses/cunix/ex01/Makefile index 58d01e16..2a9d87aa 100644 --- a/courses/cunix/ex01/Makefile +++ b/courses/cunix/ex01/Makefile @@ -7,6 +7,8 @@ LDFLAGS += -Llib CFLAGS += -Iinclude -Wall -Wextra -Werror -g +ccflags-y := -std=c99 + SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) From 7a6c0fc22d242267b0c1879f8e06a0c74d4b7ba5 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 16:50:33 +0300 Subject: [PATCH 02/23] add my_strlen implementation --- courses/cunix/ex01/src/my_strlen.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 courses/cunix/ex01/src/my_strlen.c diff --git a/courses/cunix/ex01/src/my_strlen.c b/courses/cunix/ex01/src/my_strlen.c new file mode 100644 index 00000000..c10939fb --- /dev/null +++ b/courses/cunix/ex01/src/my_strlen.c @@ -0,0 +1,29 @@ +/* + * ===================================================================================== + * + * Filename: my_strlen.c + * + * Description: own implementation of std strel function + * + * Version: 1.0 + * Created: 08/20/2021 12:48:09 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +unsigned int my_strlen(char *str) +{ + char *ch = str; + unsigned int len = 0; + while (*(ch++) != '\0') + { + len++; + } + return len; +} +/* ----- end of function my_strlen ----- */ From ee0b983d76b55fada75d1c1344216259c70c831b Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 16:53:12 +0300 Subject: [PATCH 03/23] add c99 flag --- courses/cunix/ex02/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/courses/cunix/ex02/Makefile b/courses/cunix/ex02/Makefile index 36920cba..44365c31 100644 --- a/courses/cunix/ex02/Makefile +++ b/courses/cunix/ex02/Makefile @@ -7,6 +7,8 @@ LDFLAGS += -Llib CFLAGS += -Iinclude -Wall -Wextra -Werror -g +ccflags-y := -std=c99 + SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) From 409722499e0b6a967fa6f0efe255b70fc1833b22 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 17:38:32 +0300 Subject: [PATCH 04/23] Generalize string comparison test results Now all values bigger or less than 0 are supported as a result of comparison, previously supported results were -1, 0, 1 --- courses/cunix/ex02/src/test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/courses/cunix/ex02/src/test.c b/courses/cunix/ex02/src/test.c index 97a51a8c..5b63d691 100644 --- a/courses/cunix/ex02/src/test.c +++ b/courses/cunix/ex02/src/test.c @@ -10,15 +10,15 @@ void test_small() assert(my_strcmp("", "") == 0); assert(my_strcmp("A", "A") == 0); assert(my_strcmp("AB", "AB") == 0); - assert(my_strcmp("AB", "AC") == -1); - assert(my_strcmp("AB", "AA") == 1); - assert(my_strcmp("AB", "AD") == -1); + assert(my_strcmp("AB", "AC") < 0); + assert(my_strcmp("AB", "AA") > 0); + assert(my_strcmp("AB", "AD") < 0); } void test_long() { assert(my_strcmp("HELLO WORLD", "HELLA WORLD") > 0); - assert(my_strcmp("HELLO WORLD", "HELL WORLD") == 1); + assert(my_strcmp("HELLO WORLD", "HELL WORLD") > 0); } int main() From a69b0c579ae896159edfa5baa104ee335b380c20 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 17:38:58 +0300 Subject: [PATCH 05/23] Implement my_strcmp function --- courses/cunix/ex02/src/my_strcmp.c | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 courses/cunix/ex02/src/my_strcmp.c diff --git a/courses/cunix/ex02/src/my_strcmp.c b/courses/cunix/ex02/src/my_strcmp.c new file mode 100644 index 00000000..db3bd10c --- /dev/null +++ b/courses/cunix/ex02/src/my_strcmp.c @@ -0,0 +1,33 @@ +/* + * ===================================================================================== + * + * Filename: my_strcmp.c + * + * Description: std strcmp implementation + * + * Version: 1.0 + * Created: 08/20/2021 04:53:28 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + + +int my_strcmp(char *str1, char *str2) { + char *ch1 = str1, *ch2 = str2; + + while (*(ch1) != '\0' && *(ch2) != '\0') { + if (*(ch1) != *(ch2)) { + return *(ch1) - *(ch2); + } + ch1++; + ch2++; + } + + return *(ch1) - *(ch2); +} +/* ----- end of function my_strcmp ----- */ From 574e2f8947a5d48c385d6b8aff1766903c98c089 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 19:59:54 +0300 Subject: [PATCH 06/23] Add c99 flag --- courses/cunix/ex03/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/courses/cunix/ex03/Makefile b/courses/cunix/ex03/Makefile index 8183d82f..1af3c7f8 100644 --- a/courses/cunix/ex03/Makefile +++ b/courses/cunix/ex03/Makefile @@ -7,6 +7,8 @@ LDFLAGS += -Llib CFLAGS += -Iinclude -Wall -Wextra -Werror -g +ccflags-y := -std=c99 + SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) From 479eb2a5cf480fd09767d1299fee86ee218fd844 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 20:14:51 +0300 Subject: [PATCH 07/23] Implement my_strcpy --- courses/cunix/ex03/src/my_strcpy.c | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 courses/cunix/ex03/src/my_strcpy.c diff --git a/courses/cunix/ex03/src/my_strcpy.c b/courses/cunix/ex03/src/my_strcpy.c new file mode 100644 index 00000000..76abfaf4 --- /dev/null +++ b/courses/cunix/ex03/src/my_strcpy.c @@ -0,0 +1,42 @@ +/* + * ===================================================================================== + * + * Filename: my_strcpy.c + * + * Description: own std strcpy implementation + * + * Version: 1.0 + * Created: 08/20/2021 08:00:40 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +/* + * === FUNCTION ====================================================================== + * Name: my_strcpy + * Description: std strcpy implementation. + * Note: the user must ensure that 'dest' buffer has enough space to have 'src' copied into it, otherwise undefined behavior! + * ===================================================================================== + */ + +#include + +char *my_strcpy(char *dest, char *src) +{ + size_t i = 0; + + while (src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + + dest[i] = '\0'; + return dest; +} +/* ----- end of function my_strcpy ----- */ From 3a1fcb9c903825b4459b334d961968531e92b0f2 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 20:17:38 +0300 Subject: [PATCH 08/23] Make test messages more consistent --- courses/cunix/ex03/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/courses/cunix/ex03/Makefile b/courses/cunix/ex03/Makefile index 1af3c7f8..cbc12182 100644 --- a/courses/cunix/ex03/Makefile +++ b/courses/cunix/ex03/Makefile @@ -29,7 +29,7 @@ fclean: clean re: fclean all test: re - @(./$(NAME) && echo "Test success" || echo "Test Fails") + @(./$(NAME) && echo "Test Successes" || echo "Test Fails") debug: $(NAME) valgrind -v --leak-check=full --track-origins=yes ./$(NAME) From 25e2a46441498532897ea8e57677e39ee8c226e9 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 21:35:01 +0300 Subject: [PATCH 09/23] remove unnecessary var --- courses/cunix/ex01/src/my_strlen.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/courses/cunix/ex01/src/my_strlen.c b/courses/cunix/ex01/src/my_strlen.c index c10939fb..401e17f9 100644 --- a/courses/cunix/ex01/src/my_strlen.c +++ b/courses/cunix/ex01/src/my_strlen.c @@ -18,9 +18,8 @@ unsigned int my_strlen(char *str) { - char *ch = str; unsigned int len = 0; - while (*(ch++) != '\0') + while (*(str++) != '\0') { len++; } From bbe0ef1ef14a0fc29afe985942529a0223a88157 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 21:36:36 +0300 Subject: [PATCH 10/23] removed unnecessary vars --- courses/cunix/ex02/src/my_strcmp.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/courses/cunix/ex02/src/my_strcmp.c b/courses/cunix/ex02/src/my_strcmp.c index db3bd10c..e21dde10 100644 --- a/courses/cunix/ex02/src/my_strcmp.c +++ b/courses/cunix/ex02/src/my_strcmp.c @@ -18,16 +18,14 @@ int my_strcmp(char *str1, char *str2) { - char *ch1 = str1, *ch2 = str2; - - while (*(ch1) != '\0' && *(ch2) != '\0') { - if (*(ch1) != *(ch2)) { - return *(ch1) - *(ch2); + while (*(str1) != '\0' && *(str2) != '\0') { + if (*(str1) != *(str2)) { + return *(str1) - *(str2); } - ch1++; - ch2++; + str1++; + str2++; } - return *(ch1) - *(ch2); + return *(str1) - *(str2); } /* ----- end of function my_strcmp ----- */ From 49d08902e978f047e82047f057c51873a216586e Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 23:19:32 +0300 Subject: [PATCH 11/23] Implement my_atoi, my_itoa --- courses/cunix/ex04/src/my_atoi.c | 57 ++++++++++++++++++++++++++++++++ courses/cunix/ex04/src/my_itoa.c | 36 ++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 courses/cunix/ex04/src/my_atoi.c create mode 100644 courses/cunix/ex04/src/my_itoa.c diff --git a/courses/cunix/ex04/src/my_atoi.c b/courses/cunix/ex04/src/my_atoi.c new file mode 100644 index 00000000..a3654c92 --- /dev/null +++ b/courses/cunix/ex04/src/my_atoi.c @@ -0,0 +1,57 @@ +/* + * ===================================================================================== + * + * Filename: my_atoi.c + * + * Description: own implementation of std atoi function + * + * Version: 1.0 + * Created: 08/20/2021 09:37:38 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + + +/* + * === FUNCTION ====================================================================== + * Name: my_atoi + * Description: converts array of chars 'nptr' to a number up to a non-digit character. Any leading spaces are skipped, the sign char is read if present. If a string can't be parsed to a number, 0 is returned. + * ===================================================================================== + */ +int my_atoi(const char *nptr) +{ + int num = 0, sign = 1, i = 0; + + // skip leading spaces + while (nptr[i] == ' ') + { + i++; + } + + // read the sign of a number if any + if (nptr[i] == '-' || nptr[i] == '+') + { + if (nptr[i] == '-') + { + sign = -1; + } + i++; + } + + // read digits one by one + while ('0' <= nptr[i] && nptr[i] <= '9') + { + num = num * 10 + (nptr[i] - '0'); + i++; + } + + num *= sign; + + return num; +} +/* ----- end of function my_atoi ----- */ diff --git a/courses/cunix/ex04/src/my_itoa.c b/courses/cunix/ex04/src/my_itoa.c new file mode 100644 index 00000000..5a520482 --- /dev/null +++ b/courses/cunix/ex04/src/my_itoa.c @@ -0,0 +1,36 @@ +/* + * ===================================================================================== + * + * Filename: my_itoa.c + * + * Description: own implementation of itoa function + * + * Version: 1.0 + * Created: 08/20/2021 10:46:17 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include +#include +#include + +/* + * === FUNCTION ====================================================================== + * Name: my_itoa + * Description: converts 'nmb' to array of char + * ===================================================================================== + */ +char *my_itoa(int nmb) +{ + // using malloc to allocate memory from the heap + char *str = malloc(sizeof nmb * CHAR_BIT + 1 + 1); + sprintf(str, "%d", nmb); + return str; +} +/* ----- end of function my_itoa ----- */ From a93e5f60e8a791c8c93dd25952f38bb5c8db96ab Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 23:25:07 +0300 Subject: [PATCH 12/23] Add c99 flag --- courses/cunix/ex04/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/courses/cunix/ex04/Makefile b/courses/cunix/ex04/Makefile index 2fa7578b..2817376f 100644 --- a/courses/cunix/ex04/Makefile +++ b/courses/cunix/ex04/Makefile @@ -7,6 +7,8 @@ LDFLAGS += -Llib CFLAGS += -Iinclude -Wall -Wextra -Werror -g +ccflags-y := -std=c99 + SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) From 4e31b2f4c8fc8b25954368a1d4f7035eff1376b8 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Fri, 20 Aug 2021 23:25:47 +0300 Subject: [PATCH 13/23] Add c99 flag --- courses/cunix/ex05/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/courses/cunix/ex05/Makefile b/courses/cunix/ex05/Makefile index 43daeaa7..ef60399d 100644 --- a/courses/cunix/ex05/Makefile +++ b/courses/cunix/ex05/Makefile @@ -9,6 +9,8 @@ LDFLAGS += -Llib CFLAGS += -Iinclude -Wall -Wextra -Werror -g +ccflags-y := -std=c99 + SRCS := $(wildcard src/*.c) OBJS := $(SRCS:.c=.o) From 8fb8d7a938d43dbf0ad4c132e9799e56c10cc8c6 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 01:12:04 +0300 Subject: [PATCH 14/23] Implement my_puts --- courses/cunix/ex05/src/my_puts.c | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 courses/cunix/ex05/src/my_puts.c diff --git a/courses/cunix/ex05/src/my_puts.c b/courses/cunix/ex05/src/my_puts.c new file mode 100644 index 00000000..ba38e8c3 --- /dev/null +++ b/courses/cunix/ex05/src/my_puts.c @@ -0,0 +1,47 @@ +/* + * ===================================================================================== + * + * Filename: my_puts.c + * + * Description: own implementation of std puts function using only write + * + * Version: 1.0 + * Created: 08/20/2021 11:26:08 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include + +/* + * === FUNCTION ====================================================================== + * Name: my_puts + * Description: writes the string s and a trailing newline to stdout. + * ===================================================================================== + */ +int my_puts(const char *s) +{ + int i = 0; + + while (s[i] != '\0') + { + if (putchar(s[i]) == EOF) + { + return EOF; + } + i++; + } + + if (putchar('\n') == EOF) + { + return EOF; + } + + return 1; +} +/* ----- end of function my_puts ----- */ From 283ec229fbb009259e8ac2cd18b9294cb689499a Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 14:23:50 +0300 Subject: [PATCH 15/23] Add c99 flag --- courses/cunix/ex07/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/courses/cunix/ex07/Makefile b/courses/cunix/ex07/Makefile index da37929f..dabbe54e 100644 --- a/courses/cunix/ex07/Makefile +++ b/courses/cunix/ex07/Makefile @@ -5,7 +5,7 @@ RM := rm -rf LDFLAGS += -Llib -CFLAGS += -Iinclude -Wall -Wextra -Werror -g +CFLAGS += -std=c99 -Iinclude -Wall -Wextra -Werror -g SRCS := $(wildcard src/*.c) From 0a16aef738991a9a4eb3db49444f483db2d3d983 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 15:50:45 +0300 Subject: [PATCH 16/23] Fix void pointer print in printf --- courses/cunix/ex07/src/test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/courses/cunix/ex07/src/test.c b/courses/cunix/ex07/src/test.c index bdb433fc..b2c6c704 100644 --- a/courses/cunix/ex07/src/test.c +++ b/courses/cunix/ex07/src/test.c @@ -7,7 +7,7 @@ void printInt(void *data) { - printf("%s\n", data); + printf("%p\n", data); } void test_destroy_push(void *data) From ef1fae70bfda69eafb27e9a19162d0ed2ca04cb4 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 15:51:05 +0300 Subject: [PATCH 17/23] Done linked list --- courses/cunix/ex07/src/linked_list.c | 172 +++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 courses/cunix/ex07/src/linked_list.c diff --git a/courses/cunix/ex07/src/linked_list.c b/courses/cunix/ex07/src/linked_list.c new file mode 100644 index 00000000..f8795b81 --- /dev/null +++ b/courses/cunix/ex07/src/linked_list.c @@ -0,0 +1,172 @@ +/* + * ===================================================================================== + * + * Filename: linked_list.c + * + * Description: linked list implementation + * + * Version: 1.0 + * Created: 08/21/2021 01:46:47 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include +#include + +typedef struct node { + void *data; + struct node *next; +} node_t; + +// Special internal function for node creation +void *_node_create(void *data, node_t *next) +{ + node_t *node = malloc(sizeof(node_t)); + + if (!node) + { + printf("Node memory allocation failed!"); + exit(1); + } + + node->data = data; + node->next = next; + + return node; +} + +node_t *list_create(void *data) +{ + return _node_create(data, NULL); +} + +void list_destroy(node_t **head, void (*fp)(void *data)) +{ + if (!head || !(*head)) + { + return; + } + + node_t *next = (*head)->next; + list_destroy(&next, fp); + (*fp)((*head)->data); + free(*head); +} + +void list_push(node_t *head, void *data) +{ + if (!head) + { + return; + } + + node_t *curr = head; + while (curr->next) + { + curr = curr->next; + } + + curr->next = _node_create(data, NULL); +} + +void list_unshift(node_t **head, void *data) +{ + if (!head || !(*head)) + { + return; + } + + *head = _node_create(data, *head); +} + +// Removes and returns the last node +void *list_pop(node_t **head) +{ + if (!head || !(*head)) + { + return NULL; + } + + // if Head is the only node, remove and return it + if ((*head)->next == NULL) + { + node_t *old_head = *head; + *head = NULL; + return old_head; + } + + // if Head has any 'next' nodes, traverse them to the last but one node + node_t *curr = *head; + while (curr->next->next) + { + curr = curr->next; + } + + node_t *popped_node = curr->next; + curr->next = NULL; + return popped_node; +} + +// Removes and returns the first node +void *list_shift(node_t **head) +{ + if (!head || !(*head)) + { + return NULL; + } + + node_t *old_head = *head; + *head = (*head)->next; // even if there is no 'next' node, we make Head points to NULL + return old_head; +} + +// Removes and returns node being pointed by 'ptr' +void *list_remove(node_t **head, node_t *ptr) +{ + if (!head || !(*head) || !ptr) + { + return NULL; + } + + node_t *curr = *head; + while (curr->next != ptr && curr->next) + { + curr = curr-> next; + } + + // node pointed by 'ptr' is not in the list + if (!curr->next) + { + return NULL; + } + + node_t *node_to_remove = curr->next; + curr->next = node_to_remove->next; + return node_to_remove; +} + +void list_print(node_t *head) +{ + node_t *curr = head; + while (curr) + { + printf("%s", (char *)(curr->data)); + curr = curr->next; + } +} + +void list_visitor(node_t *head, void (*fp)(void *data)) +{ + node_t *curr = head; + while (curr) + { + (*fp)(curr->data); + curr = curr->next; + } +} From 85445c5310a82c7bffcdeb90c03d2ef3cdb50674 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 15:53:04 +0300 Subject: [PATCH 18/23] Add c99 flag --- courses/cunix/ex08/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/courses/cunix/ex08/Makefile b/courses/cunix/ex08/Makefile index 9ba2a96a..2fce43a6 100644 --- a/courses/cunix/ex08/Makefile +++ b/courses/cunix/ex08/Makefile @@ -5,7 +5,7 @@ RM := rm -rf LDFLAGS += -Llib -CFLAGS += -Iinclude -Wall -Wextra -Werror -g +CFLAGS += -std=c99 -Iinclude -Wall -Wextra -Werror -g SRCS := $(wildcard src/*.c) From ddb53e7d501b529db550a40f006998ce5a9ccf60 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Sat, 21 Aug 2021 16:39:12 +0300 Subject: [PATCH 19/23] Done binary tree --- courses/cunix/ex08/src/binary_tree.c | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 courses/cunix/ex08/src/binary_tree.c diff --git a/courses/cunix/ex08/src/binary_tree.c b/courses/cunix/ex08/src/binary_tree.c new file mode 100644 index 00000000..ee5f6032 --- /dev/null +++ b/courses/cunix/ex08/src/binary_tree.c @@ -0,0 +1,102 @@ +/* + * ===================================================================================== + * + * Filename: binary_tree.c + * + * Description: Binary tree implementation + * + * Version: 1.0 + * Created: 08/21/2021 03:54:16 PM + * Revision: none + * Compiler: gcc + * + * Author: Nikita Sazonov (ns), actpohabt.ns@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include +#include +#include + +typedef struct node +{ + char *key; + void *data; + struct node *left; + struct node *right; +} node_t; + +node_t *allocnode() +{ + node_t *node = malloc(sizeof(node_t)); + + if (!node) + { + printf("Node memory allocation failed!"); + exit(1); + } + + return node; +} + +node_t *insert(node_t *root, char *key, void *data) +{ + if (!root) + { + node_t *root = allocnode(); + root->key = key; + root->data = data; + root->left = NULL; + root->right = NULL; + + return root; + } + + if (strcmp(data, root->data) >= 0) + { + root->left = insert(root->left, key, data); + } + else + { + root->right = insert(root->right, key, data); + } + + return root; +} + +void print_node(node_t *node) +{ + if (node) + { + printf("key: %s, data: %s\n", node->key, (char *) node->data); + } +} + +// inorder traversal +void visit_tree(node_t *node, void (*fp)(node_t *root)) +{ + if (!node) + { + return; + } + + visit_tree(node->left, fp); + (*fp)(node); + visit_tree(node->right, fp); +} + +// postorder deletion +void destroy_tree(node_t *node, void (*fdestroy)(node_t *root)) +{ + if (!node) + { + return; + } + + destroy_tree(node->left, fdestroy); + destroy_tree(node->right, fdestroy); + (*fdestroy)(node); + free(node); +} From cab0fa5ad2058a6e196243b43b498daeb1a14490 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Tue, 24 Aug 2021 21:41:18 +0300 Subject: [PATCH 20/23] Fix brackets style --- courses/cunix/ex02/src/my_strcmp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/courses/cunix/ex02/src/my_strcmp.c b/courses/cunix/ex02/src/my_strcmp.c index e21dde10..8b0db5e4 100644 --- a/courses/cunix/ex02/src/my_strcmp.c +++ b/courses/cunix/ex02/src/my_strcmp.c @@ -17,7 +17,8 @@ */ -int my_strcmp(char *str1, char *str2) { +int my_strcmp(char *str1, char *str2) +{ while (*(str1) != '\0' && *(str2) != '\0') { if (*(str1) != *(str2)) { return *(str1) - *(str2); From 3050fbd265f18a8f2483a72ff0f721a04483c037 Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Tue, 24 Aug 2021 21:43:29 +0300 Subject: [PATCH 21/23] Fix removed typedef from .c file --- courses/cunix/ex07/src/linked_list.c | 6 ------ 1 file changed, 6 deletions(-) mode change 100644 => 100755 courses/cunix/ex07/src/linked_list.c diff --git a/courses/cunix/ex07/src/linked_list.c b/courses/cunix/ex07/src/linked_list.c old mode 100644 new mode 100755 index f8795b81..44fae7f5 --- a/courses/cunix/ex07/src/linked_list.c +++ b/courses/cunix/ex07/src/linked_list.c @@ -19,12 +19,6 @@ #include #include -typedef struct node { - void *data; - struct node *next; -} node_t; - -// Special internal function for node creation void *_node_create(void *data, node_t *next) { node_t *node = malloc(sizeof(node_t)); From fc291baa394901925a49befaa787e47d32db353a Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Tue, 24 Aug 2021 22:15:03 +0300 Subject: [PATCH 22/23] Fix memory leaks, wrong function signature --- courses/cunix/ex07/src/linked_list.c | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/courses/cunix/ex07/src/linked_list.c b/courses/cunix/ex07/src/linked_list.c index 44fae7f5..28d11c5d 100755 --- a/courses/cunix/ex07/src/linked_list.c +++ b/courses/cunix/ex07/src/linked_list.c @@ -18,8 +18,9 @@ #include #include +#include "linked_list.h" -void *_node_create(void *data, node_t *next) +node_t *_node_create(void *data, node_t *next) { node_t *node = malloc(sizeof(node_t)); @@ -104,7 +105,10 @@ void *list_pop(node_t **head) node_t *popped_node = curr->next; curr->next = NULL; - return popped_node; + + void *data = popped_node->data; + free(popped_node); + return data; } // Removes and returns the first node @@ -117,21 +121,29 @@ void *list_shift(node_t **head) node_t *old_head = *head; *head = (*head)->next; // even if there is no 'next' node, we make Head points to NULL - return old_head; + + void *data = old_head->data; + free(old_head); + return data; } // Removes and returns node being pointed by 'ptr' -void *list_remove(node_t **head, node_t *ptr) +void *list_remove(node_t **head, int pos) { - if (!head || !(*head) || !ptr) + if (!head || !(*head) || pos < 0) { return NULL; } node_t *curr = *head; - while (curr->next != ptr && curr->next) + for (int i = 0; i < pos; i++) { - curr = curr-> next; + if (!curr->next) + { + return NULL; + } + + curr = curr->next; } // node pointed by 'ptr' is not in the list @@ -142,7 +154,10 @@ void *list_remove(node_t **head, node_t *ptr) node_t *node_to_remove = curr->next; curr->next = node_to_remove->next; - return node_to_remove; + + void *data = node_to_remove->data; + free(node_to_remove); + return data; } void list_print(node_t *head) @@ -160,7 +175,7 @@ void list_visitor(node_t *head, void (*fp)(void *data)) node_t *curr = head; while (curr) { - (*fp)(curr->data); + fp(curr->data); curr = curr->next; } } From d1bef877f257399800b661731ae4015a812f782f Mon Sep 17 00:00:00 2001 From: actpohabtNS Date: Tue, 24 Aug 2021 22:28:12 +0300 Subject: [PATCH 23/23] Fix typedef removed, nice usage of function pointers --- courses/cunix/ex08/src/binary_tree.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/courses/cunix/ex08/src/binary_tree.c b/courses/cunix/ex08/src/binary_tree.c index ee5f6032..2fd14823 100644 --- a/courses/cunix/ex08/src/binary_tree.c +++ b/courses/cunix/ex08/src/binary_tree.c @@ -19,14 +19,7 @@ #include #include #include - -typedef struct node -{ - char *key; - void *data; - struct node *left; - struct node *right; -} node_t; +#include "binary_tree.h" node_t *allocnode() { @@ -83,7 +76,7 @@ void visit_tree(node_t *node, void (*fp)(node_t *root)) } visit_tree(node->left, fp); - (*fp)(node); + fp(node); visit_tree(node->right, fp); } @@ -97,6 +90,6 @@ void destroy_tree(node_t *node, void (*fdestroy)(node_t *root)) destroy_tree(node->left, fdestroy); destroy_tree(node->right, fdestroy); - (*fdestroy)(node); + fdestroy(node); free(node); }