From: Mike D. Lowis Date: Fri, 3 Jun 2011 16:38:56 +0000 (-0400) Subject: Fixed bug in LL_Get and added LL_Length function X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=1681e5b27373c2a7d3d373e39f3867618853b447;p=projs%2Flibcds.git Fixed bug in LL_Get and added LL_Length function --- diff --git a/src/linked_list/linked_list.c b/src/linked_list/linked_list.c index 3cab7bb..a0df8ac 100644 --- a/src/linked_list/linked_list.c +++ b/src/linked_list/linked_list.c @@ -43,7 +43,7 @@ LinkedList_T* LL_Get( LinkedList_T* list, int index ) int current = 0; LinkedList_T* node = list; LinkedList_T* indexed_node = NULL; - while ((node != NULL) && (node->next != NULL)) + while ((node != NULL)) { if ( current == index ) { @@ -106,3 +106,14 @@ void LL_Free( LinkedList_T* list, BOOL free_contents) } } +U32 LL_Length(LinkedList_T* list) +{ + U32 length = 0; + LinkedList_T* item = list; + for ( item = list; item != NULL; item = item->next ) + { + length++; + } + return length; +} + diff --git a/src/linked_list/linked_list.h b/src/linked_list/linked_list.h index 881b1aa..551a1da 100644 --- a/src/linked_list/linked_list.h +++ b/src/linked_list/linked_list.h @@ -104,6 +104,16 @@ void LL_Delete( LinkedList_T* list, int index, BOOL free_contents); * */ void LL_Free( LinkedList_T* list, BOOL free_contents); -#define LL_FOREACH(item,list) for( item = list; item != NULL; item = item->next ) +/** + * @brief Returns the number of elements in the list. + * + * Loops through the supplied list and returns a count of the number of elements + * contained in the list. + * + * @param list The list to be counted. + * + * @return The number of elements in the list. + **/ +U32 LL_Length(LinkedList_T* list); #endif