void sll_free(sll_t* list, int free_contents)
{
- if( NULL != list )
+ sll_node_t* node = list->head;
+ while( NULL != node )
{
- sll_node_t* node = list->head;
- while( NULL != node )
- {
- sll_node_t* next = node->next;
- sll_free_node( node, free_contents );
- node = next;
- }
- free( list );
+ sll_node_t* next = node->next;
+ sll_free_node( node, free_contents );
+ node = next;
}
+ free( list );
}
void sll_free_node(sll_node_t* node, int free_contents)
{
- if( NULL != node )
+ if( free_contents )
{
- if( 1 == free_contents )
- {
- free( node->contents );
- }
- free( node );
+ free( node->contents );
}
+ free( node );
}
sll_node_t* sll_front( sll_t* list )
{
- sll_node_t* node = NULL;
- if( NULL != list )
- {
- node = list->head;
- }
- return node;
+ return list->head;
}
sll_node_t* sll_back( sll_t* list )
{
- sll_node_t* node = NULL;
- if( NULL != list )
- {
- node = list->tail;
- }
- return node;
+ return list->tail;
}
unsigned int sll_size(sll_t* list)
{
unsigned int length = 0;
- if( NULL != list)
+ sll_node_t* node = list->head;
+ while( NULL != node )
{
- sll_node_t* node = list->head;
- while( NULL != node )
- {
- node = node->next;
- length++;
- }
+ node = node->next;
+ length++;
}
return length;
}
int sll_empty(sll_t* list)
{
- return ((NULL == list) || ((NULL == list->head) && (NULL == list->tail)));
+ return ((NULL == list->head) && (NULL == list->tail));
}
sll_node_t* sll_at(sll_t* list, unsigned int index)
{
sll_node_t* node = NULL;
- if( NULL != list )
+ unsigned int cur_index = 0;
+ sll_node_t* cur_node = list->head;
+ while( NULL != cur_node )
{
- unsigned int cur_index = 0;
- sll_node_t* cur_node = list->head;
- while( NULL != cur_node )
+ if( cur_index == index )
{
- if( cur_index == index )
- {
- node = cur_node;
- break;
- }
- cur_node = cur_node->next;
- cur_index++;
+ node = cur_node;
+ break;
}
+ cur_node = cur_node->next;
+ cur_index++;
}
return node;
}
sll_node_t* sll_push_front( sll_t* list, void* contents )
{
- sll_node_t* node = NULL;
- if( NULL != list )
+ sll_node_t* node = sll_new_node( contents );
+ node->next = list->head;
+ list->head = node;
+ if( NULL == list->tail )
{
- node = sll_new_node( contents );
- node->next = list->head;
- list->head = node;
- if( NULL == list->tail )
- {
- list->tail = node;
- }
+ list->tail = node;
}
return node;
}
sll_node_t* sll_push_back( sll_t* list, void* contents )
{
- sll_node_t* node = NULL;
- if( NULL != list )
+ sll_node_t* node = sll_new_node( contents );
+ node->next = NULL;
+ if( NULL == list->tail )
{
- node = sll_new_node( contents );
- node->next = NULL;
- if( NULL == list->tail )
- {
- list->head = node;
- list->tail = node;
- }
- else
- {
- list->tail->next = node;
- list->tail = node;
- }
+ list->head = node;
+ list->tail = node;
+ }
+ else
+ {
+ list->tail->next = node;
+ list->tail = node;
}
return node;
}
sll_node_t* sll_pop_front( sll_t* list )
{
sll_node_t* node = NULL;
- if( (NULL != list) && (NULL != list->head) )
+ if( NULL != list->head )
{
node = list->head;
list->head = node->next;
sll_node_t* sll_pop_back( sll_t* list )
{
sll_node_t* node = NULL;
- if( NULL != list )
+ if ( list->head == list->tail )
{
- if ( list->head == list->tail )
- {
- node = list->head;
- list->head = NULL;
- list->tail = NULL;
- }
- else
+ node = list->head;
+ list->head = NULL;
+ list->tail = NULL;
+ }
+ else
+ {
+ sll_node_t* next_tail = list->head;
+ while( next_tail->next != list->tail )
{
- sll_node_t* next_tail = list->head;
- while( next_tail->next != list->tail )
- {
- next_tail = next_tail->next;
- }
- node = next_tail->next;
- next_tail->next = NULL;
- list->tail = next_tail;
+ next_tail = next_tail->next;
}
+ node = next_tail->next;
+ next_tail->next = NULL;
+ list->tail = next_tail;
}
return node;
}
sll_t* sll_clear(sll_t* list, int free_contents)
{
- if(NULL != list)
+ sll_node_t* node = list->head;
+ while(NULL != node)
{
- sll_node_t* node = list->head;
- while(NULL != node)
- {
- sll_node_t* next = node->next;
- sll_free_node(node,free_contents);
- node = next;
- }
- list->head = NULL;
- list->tail = NULL;
+ sll_node_t* next = node->next;
+ sll_free_node(node,free_contents);
+ node = next;
}
+ list->head = NULL;
+ list->tail = NULL;
return list;
}
-
//-------------------------------------------------------------------------
// Test sll_free function
//-------------------------------------------------------------------------
- TEST(Verify_sll_free_does_nothing_on_null_pointer)
- {
- sll_free( NULL, 0 );
- }
-
TEST(Verify_sll_free_frees_the_given_empty_list)
{
sll_t* list = sll_new();
//-------------------------------------------------------------------------
// Test sll_free_node function
//-------------------------------------------------------------------------
- TEST(Verify_sll_free_node_does_nothing_on_null_pointer)
- {
- sll_free_node( NULL, 0 );
- }
-
TEST(Verify_sll_free_node_frees_the_given_node)
{
sll_node_t* node = sll_new_node( NULL );
//-------------------------------------------------------------------------
// Test sll_front function
//-------------------------------------------------------------------------
- TEST(Verify_sll_front_returns_NULL_if_list_is_NULL)
- {
- CHECK( NULL == sll_front( NULL ) );
- }
-
TEST(Verify_sll_front_returns_NULL_if_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_back function
//-------------------------------------------------------------------------
- TEST(Verify_sll_back_returns_NULL_if_list_is_NULL)
- {
- CHECK( NULL == sll_back( NULL ) );
- }
-
TEST(Verify_sll_back_returns_NULL_if_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_size function
//-------------------------------------------------------------------------
- TEST(Verify_sll_size_returns_0_when_passed_null_pointer)
- {
- CHECK( 0 == sll_size(NULL) );
- }
-
TEST(Verify_sll_size_returns_0_when_list_is_empty)
{
sll_t* list = sll_new();
//-------------------------------------------------------------------------
// Test sll_empty function
//-------------------------------------------------------------------------
- TEST(Verify_sll_empty_returns_true_when_passed_null_pointer)
- {
- CHECK( 1 == sll_empty( NULL ) );
- }
-
TEST(Verify_sll_empty_returns_true_when_head_and_tail_are_null)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_at function
//-------------------------------------------------------------------------
- TEST(Verify_sll_at_returns_NULL_on_null_pointer)
- {
- CHECK( NULL == sll_at( NULL, 0 ) );
- }
-
TEST(Verify_sll_at_returns_NULL_when_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_push_front function
//-------------------------------------------------------------------------
- TEST(Verify_sll_push_front_returns_null_if_list_is_null)
- {
- CHECK( NULL == sll_push_front( NULL, NULL ) );
- }
-
TEST(Verify_sll_push_front_pushes_to_empty_list)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_push_back function
//-------------------------------------------------------------------------
- TEST(Verify_sll_push_back_returns_null_if_list_is_null)
- {
- CHECK( NULL == sll_push_back( NULL, NULL ) );
- }
-
TEST(Verify_sll_push_back_pushes_to_empty_list)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_pop_front function
//-------------------------------------------------------------------------
- TEST(Verify_pop_front_returns_null_if_list_is_null)
- {
- CHECK( NULL == sll_pop_front( NULL ) );
- }
-
TEST(Verify_pop_front_returns_null_if_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_pop_back function
//-------------------------------------------------------------------------
- TEST(Verify_pop_back_does_nothing_if_list_is_null)
- {
- CHECK( NULL == sll_pop_back( NULL ) );
- }
-
TEST(Verify_pop_back_does_nothing_if_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_insert function
//-------------------------------------------------------------------------
- TEST(Verify_insert_does_nothing_if_list_is_null)
- {
- CHECK( NULL == sll_insert( NULL, 0, (void*)0x1234 ) );
- }
-
TEST(Verify_insert_should_insert_into_empty_list)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_delete function
//-------------------------------------------------------------------------
- TEST(Verify_delete_does_nothing_if_list_is_null)
- {
- CHECK( NULL == sll_delete( NULL, 0, 0 ) );
- }
-
TEST(Verify_delete_does_nothing_if_list_is_empty)
{
sll_t list = { NULL, NULL };
//-------------------------------------------------------------------------
// Test sll_clear function
//-------------------------------------------------------------------------
- TEST(Verify_sll_clear_does_nothing_if_passed_a_NULL_pointer)
- {
- CHECK( NULL == sll_clear(NULL,0) );
- }
-
TEST(Verify_sll_clear_does_nothing_for_an_empty_list)
{
sll_t* list = sll_new();