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) diff --git a/courses/cunix/ex01/src/my_strlen.c b/courses/cunix/ex01/src/my_strlen.c new file mode 100644 index 00000000..401e17f9 --- /dev/null +++ b/courses/cunix/ex01/src/my_strlen.c @@ -0,0 +1,28 @@ +/* + * ===================================================================================== + * + * 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) +{ + unsigned int len = 0; + while (*(str++) != '\0') + { + len++; + } + return len; +} +/* ----- end of function my_strlen ----- */ 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) diff --git a/courses/cunix/ex02/src/my_strcmp.c b/courses/cunix/ex02/src/my_strcmp.c new file mode 100644 index 00000000..8b0db5e4 --- /dev/null +++ b/courses/cunix/ex02/src/my_strcmp.c @@ -0,0 +1,32 @@ +/* + * ===================================================================================== + * + * 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) +{ + while (*(str1) != '\0' && *(str2) != '\0') { + if (*(str1) != *(str2)) { + return *(str1) - *(str2); + } + str1++; + str2++; + } + + return *(str1) - *(str2); +} +/* ----- end of function my_strcmp ----- */ 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() diff --git a/courses/cunix/ex03/Makefile b/courses/cunix/ex03/Makefile index 8183d82f..cbc12182 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) @@ -27,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) 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 ----- */ 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) 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 ----- */ 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) 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 ----- */ 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) diff --git a/courses/cunix/ex07/src/linked_list.c b/courses/cunix/ex07/src/linked_list.c new file mode 100755 index 00000000..28d11c5d --- /dev/null +++ b/courses/cunix/ex07/src/linked_list.c @@ -0,0 +1,181 @@ +/* + * ===================================================================================== + * + * 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 +#include "linked_list.h" + +node_t *_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; + + void *data = popped_node->data; + free(popped_node); + return data; +} + +// 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 + + void *data = old_head->data; + free(old_head); + return data; +} + +// Removes and returns node being pointed by 'ptr' +void *list_remove(node_t **head, int pos) +{ + if (!head || !(*head) || pos < 0) + { + return NULL; + } + + node_t *curr = *head; + for (int i = 0; i < pos; i++) + { + if (!curr->next) + { + return NULL; + } + + 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; + + void *data = node_to_remove->data; + free(node_to_remove); + return data; +} + +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; + } +} 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) 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) diff --git a/courses/cunix/ex08/src/binary_tree.c b/courses/cunix/ex08/src/binary_tree.c new file mode 100644 index 00000000..2fd14823 --- /dev/null +++ b/courses/cunix/ex08/src/binary_tree.c @@ -0,0 +1,95 @@ +/* + * ===================================================================================== + * + * 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 +#include "binary_tree.h" + +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); +}