From 56e195c9b4dd2b871db786a713c67374455b83e8 Mon Sep 17 00:00:00 2001 From: wael Date: Wed, 16 Feb 2022 13:45:08 +0200 Subject: [PATCH] Streamlined single linked lists some more. --- linked_lists/single_linked_list.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/linked_lists/single_linked_list.c b/linked_lists/single_linked_list.c index a543ebf..ac913bb 100644 --- a/linked_lists/single_linked_list.c +++ b/linked_lists/single_linked_list.c @@ -160,7 +160,7 @@ static inline void* read_node_i_single_linked_list(linked_list_t *list, size_t i /* Check if dealing with the head. */ if (!i) - return list->head->data; + return (list->head) ? list->head->data : NULL; /* Loop and read. */ node_t *current = list->head; @@ -168,7 +168,7 @@ static inline void* read_node_i_single_linked_list(linked_list_t *list, size_t i current = current->next; /* Got to the requested node, return the stored data. */ - return current->data; + return (current) ? current->data : NULL; } /* This function retrieves a pointer to the ith node, returns NULL on failure. */ @@ -340,7 +340,7 @@ static inline void remove_node_head_data_single_linked_list(linked_list_t *list, /* This function reads the data at the last node, will return NULL on invalid input or first node being NULL. */ static inline void* read_node_tail_single_linked_list(linked_list_t *list) { /* Sanity check. */ - assert(list && (list->head)); + assert(list); /* Delegate. */ return read_node_i_single_linked_list(list, list->length - 1); @@ -350,7 +350,7 @@ static inline void* read_node_tail_single_linked_list(linked_list_t *list) { /* Sets the error code in error (instead of returning it). */ static inline node_t* get_node_tail_single_linked_list(linked_list_t *list) { /* Sanity check. */ - assert(list && (list->head)); + assert(list); /* Delegate. */ return get_node_i_single_linked_list(list, list->length - 1);