}
}
+sll_node_t* sll_front( sll_t* list )
+{
+ sll_node_t* node = NULL;
+ if( NULL != list )
+ {
+ node = list->head;
+ }
+ return node;
+}
+
+sll_node_t* sll_back( sll_t* list )
+{
+ sll_node_t* node = NULL;
+ if( NULL != list )
+ {
+ node = list->tail;
+ }
+ return node;
+}
+
unsigned int sll_length(sll_t* list)
{
unsigned int length = 0;
sll_node_t* sll_pop_back( sll_t* list )
{
sll_node_t* node = NULL;
- if( (NULL != list) && (NULL != list->tail) )
+ if( NULL != list )
{
+ if ( list->head == list->tail )
+ {
+ node = list->head;
+ list->head = NULL;
+ list->tail = NULL;
+ }
+ else
+ {
+ 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;
+ }
}
return node;
}
sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents)
{
- return 0;
+ sll_node_t* new_node = NULL;
+ if( NULL == list )
+ {
+ if( 0 == index )
+ {
+ new_node = sll_push_front( list, contents );
+ }
+ else
+ {
+ sll_node_t* prev_node = sll_index( list, index - 1 );
+ if( NULL == prev_node )
+ {
+ sll_node_t* next_node = prev_node->next;
+ new_node = sll_new_node( contents );
+ new_node = next_node;
+ prev_node->next = new_node;
+ if( NULL == next_node )
+ {
+ list->tail = new_node;
+ }
+ }
+ }
+ }
+ return new_node;
}
sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents)
{
- return 0;
+ sll_node_t* new_node = NULL;
+
+ return new_node;
}
sll_free_node( node, 1 );
}
+ //-------------------------------------------------------------------------
+ // 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 };
+ CHECK( NULL == sll_front( &list ) );
+ }
+
+ TEST(Verify_sll_front_returns_the_head_of_the_list)
+ {
+ sll_node_t node2 = { NULL, NULL };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node2 };
+ CHECK( &node1 == sll_front( &list ) );
+ }
+
+ //-------------------------------------------------------------------------
+ // 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 };
+ CHECK( NULL == sll_back( &list ) );
+ }
+
+ TEST(Verify_sll_back_returns_the_tail_of_the_list)
+ {
+ sll_node_t node2 = { NULL, NULL };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node2 };
+ CHECK( &node2 == sll_back( &list ) );
+ }
+
//-------------------------------------------------------------------------
// Test sll_length function
//-------------------------------------------------------------------------
CHECK( &node2 == list.tail );
}
+ TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_3)
+ {
+ sll_node_t node3 = { NULL, NULL };
+ sll_node_t node2 = { NULL, &node3 };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node3 };
+ CHECK( &node1 == sll_pop_front( &list ) );
+ CHECK( &node2 == list.head );
+ CHECK( &node3 == list.tail );
+ }
+
//-------------------------------------------------------------------------
// Test sll_pop_back function
//-------------------------------------------------------------------------
TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1)
{
- CHECK(false);
+ sll_node_t node1 = { NULL, NULL };
+ sll_t list = { &node1, &node1 };
+ CHECK( &node1 == sll_pop_back( &list ) );
+ CHECK( NULL == list.head );
+ CHECK( NULL == list.tail );
}
TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2)
{
- CHECK(false);
+ sll_node_t node2 = { NULL, NULL };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node2 };
+ CHECK( &node2 == sll_pop_back( &list ) );
+ CHECK( &node1 == list.head );
+ CHECK( &node1 == list.tail );
+ }
+
+ TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_3)
+ {
+ sll_node_t node3 = { NULL, NULL };
+ sll_node_t node2 = { NULL, &node3 };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node3 };
+ CHECK( &node3 == sll_pop_back( &list ) );
+ CHECK( &node1 == list.head );
+ CHECK( &node2 == list.tail );
}
//-------------------------------------------------------------------------