From 314634df3e6a39db44d1894a288db7457cce18a7 Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Wed, 19 Mar 2025 09:49:52 +0100 Subject: [PATCH] Added memory allocation, validation, and size functions --- simple_singly_linked_list.c | 76 ++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/simple_singly_linked_list.c b/simple_singly_linked_list.c index a89ffc9..b869732 100644 --- a/simple_singly_linked_list.c +++ b/simple_singly_linked_list.c @@ -32,6 +32,7 @@ */ #include "simple_singly_linked_list.h" +#include "allocator.h" /** The type of list nodes */ typedef struct IntListNodeData* IntListNode; @@ -44,7 +45,7 @@ struct IntListNodeData { /** The implementation of list data */ struct IntListData { - IntList list; + IntListNode head; }; @@ -53,11 +54,14 @@ struct IntListData { /* abstract away and generalize also memory allocation for list nodes */ static IntListNode list_obtain_node(int data) { - return 0; + IntListNode node = (IntListNode)alloc_mem(sizeof(struct IntListNodeData)); + node->data = 0; + node->next = 0; + return node; } static void list_release_node(IntListNode node) { - return; + free_mem(node); } /* optional: implement a function for printing the content of the list - may be useful for debugging */ @@ -79,7 +83,9 @@ void list_dump(char* prefix, IntList list) { * @return IntList The list instance or 0, if no list could by instantiated. */ IntList list_obtain() { - return 0; + IntList intList = (IntList)alloc_mem(sizeof(struct IntListData)); + intList->head = 0; + return intList; } /** @@ -94,7 +100,7 @@ IntList list_obtain() { * is set to 0, if the list was successfully released, otherwise it is left untouched. */ void list_release(IntList* p_list) { - return; + free_mem(p_list); } /** @@ -104,7 +110,12 @@ void list_release(IntList* p_list) { * @return `True` if the list is valid, false otherwise. */ bool list_is_valid(IntList list) { - return 0; + if (list == 0) { + return false; + } + + // some checks + return true; } /** @@ -114,7 +125,16 @@ bool list_is_valid(IntList list) { * @return `False` if the list contains one or more items, `true` otherwise. */ bool list_is_empty(IntList list) { - return 0; + if (!list_is_valid) { + return true; + } + + if(list->head == 0) { + return true; + } + + // some checks + return false; } /** @@ -124,7 +144,18 @@ bool list_is_empty(IntList list) { * @return The number of values the list contains. */ int list_get_size(IntList list) { - return 0; + if(!list_is_valid) { + return -1; + } + + int size = 0; + IntListNode current = list->head; + while(current->next != 0) { + size++; + current = current->next; + } + + return size; } /** @@ -137,7 +168,20 @@ int list_get_size(IntList list) { * `false ` otherwise. */ bool list_contains(IntList list, int value) { - return 0; + if(!list_is_valid || list_is_empty) { + return false; + } + + IntListNode current = list->head; + while(current->next != 0) { + if(current->data == value) { + return true; + } + + current = current->next; + } + + return false; } /** @@ -149,6 +193,20 @@ bool list_contains(IntList list, int value) { * is not available. */ int list_get_at(IntList list, unsigned int index) { + if(!list_is_valid || list_is_empty) { + return 0; + } + + int i = 0; + IntListNode current = list->head; + while(current->next != 0) { + if(i == index) { + return current->data; + } + + current = current->next; + } + return 0; }