From 131a59dc75a8c332cd4464f31794422ddd82b51a Mon Sep 17 00:00:00 2001 From: Lorenzo Rocca Date: Tue, 14 Oct 2025 11:51:49 +0200 Subject: [PATCH 1/2] Create Easy List Node algorithms All the utilities to manage a list made in C, with the possibility to customize it in the future. Supports: - Adding - Deleting (with memo-optimizations) - Querying - Updating - Creation utility --- C/Linked-List/Easy List Node algorithms | 140 ++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 C/Linked-List/Easy List Node algorithms diff --git a/C/Linked-List/Easy List Node algorithms b/C/Linked-List/Easy List Node algorithms new file mode 100644 index 000000000..2be7cdbc6 --- /dev/null +++ b/C/Linked-List/Easy List Node algorithms @@ -0,0 +1,140 @@ +#include +#include + +typedef struct s_node { + int value; + struct s_node* next; +} ListNode; + +ListNode* get(int index, ListNode* node) { + if (node == nullptr || index < 0) { + return nullptr; + } + + if (index == 0) { + return node; + } + + return get(--index, node->next); +} + +void set(ListNode* node, int index, int value) { + if (node == nullptr || index < 0) { + return; + } + + if (index == 0) { + node->value = value; + return; + } + + set(node->next, --index, value); +} + +void insert_top(ListNode** list, ListNode* node) { + node->next = *list; + *list = node; +} + +void insert_bottom(ListNode* list, ListNode* node) { + if (list->next == nullptr) { + list->next = node; + return; + } + + insert_bottom(list->next, node); +} + +void insert_rec(ListNode* currentNode, ListNode* node, int index) { + if (currentNode == nullptr) { + return; + } + + if (index - 1 == 0) { + node->next = currentNode->next; + currentNode->next = node; + return; + } + + insert_rec(currentNode->next, node, --index); +} + +void insert_to_list(ListNode** list, ListNode* node, const int index) { + if (index < 0) + return; + + if (index == 0) { + insert_top(list, node); + return; + } + + insert_rec(*list, node, index); +} + +void remove_top(ListNode** list) { + ListNode* tempNode = *list; + *list = (*list)->next; + free(tempNode); +} + +void remove_bottom(ListNode* node) { + if (node->next->next == nullptr) { + ListNode* tempNode = node->next; + node->next = nullptr; + free(tempNode); + return; + } + + remove_bottom(node->next); +} + +void remove_rec(ListNode* currentNode, int index) { + if (index - 1 == 0) { + ListNode* tempNode = currentNode->next; + currentNode->next = tempNode->next; + free(tempNode); + return; + } + + remove_rec(currentNode->next, --index); +} + +void remove_from_list(ListNode** list, const int index) { + if (index < 0) { + return; + } + + if (index == 0) { + remove_top(list); + return; + } + + remove_rec(*list, index); +} + +ListNode* create_list(const int value) { + ListNode* node = malloc(sizeof(ListNode)); + node->value = value; + node->next = nullptr; + return node; +} + +int main(void) { + ListNode* list = create_list(1); + ListNode* n2 = create_list(2); + ListNode* n3 = create_list(3); + ListNode* n4 = create_list(4); + ListNode* n5 = create_list(5); + + insert_to_list(&list, n2, 1); + insert_to_list(&list, n3, 2); + insert_to_list(&list, n4, 3); + insert_to_list(&list, n5, 4); + + free(list); + free(n2); + free(n3); + free(n4); + free(n5); + return 0; +} From 9df97febeb2669e64116a5a4230150fbfbe7a03a Mon Sep 17 00:00:00 2001 From: Lorenzo Rocca Date: Tue, 14 Oct 2025 17:48:54 +0200 Subject: [PATCH 2/2] Fix little issues --- C/Linked-List/Easy List Node algorithms | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/C/Linked-List/Easy List Node algorithms b/C/Linked-List/Easy List Node algorithms index 2be7cdbc6..0cb106d01 100644 --- a/C/Linked-List/Easy List Node algorithms +++ b/C/Linked-List/Easy List Node algorithms @@ -89,6 +89,10 @@ void remove_bottom(ListNode* node) { } void remove_rec(ListNode* currentNode, int index) { + if (currentNode == nullptr) { + return; + } + if (index - 1 == 0) { ListNode* tempNode = currentNode->next; currentNode->next = tempNode->next; @@ -118,23 +122,3 @@ ListNode* create_list(const int value) { node->next = nullptr; return node; } - -int main(void) { - ListNode* list = create_list(1); - ListNode* n2 = create_list(2); - ListNode* n3 = create_list(3); - ListNode* n4 = create_list(4); - ListNode* n5 = create_list(5); - - insert_to_list(&list, n2, 1); - insert_to_list(&list, n3, 2); - insert_to_list(&list, n4, 3); - insert_to_list(&list, n5, 4); - - free(list); - free(n2); - free(n3); - free(n4); - free(n5); - return 0; -}