{
node = sll_new_node( contents );
node->next = list->head;
+ list->head = node;
if( NULL == list->tail )
{
list->tail = node;
else
{
list->tail->next = node;
+ list->tail = node;
}
}
return node;
}
-sll_node_t* sll_pop_front( sll_t* list, int free_contents )
+sll_node_t* sll_pop_front( sll_t* list )
{
- return 0;
+ sll_node_t* node = NULL;
+ if( (NULL != list) && (NULL != list->head) )
+ {
+ node = list->head;
+ list->head = node->next;
+ if( node == list->tail )
+ {
+ list->tail = NULL;
+ }
+ }
+ return node;
}
-sll_node_t* sll_pop_back( sll_t* list, int free_contents )
+sll_node_t* sll_pop_back( sll_t* list )
{
- return 0;
+ sll_node_t* node = NULL;
+ if( (NULL != list) && (NULL != list->tail) )
+ {
+ }
+ return node;
}
sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents)
* @brief Removes and returns a pointer to the first element of the list.
*
* This function removes the first node from the list and frees it's associated
- * memory. If free_contents is passed a non-zero value then it's contents
- * pointer is also freed. The second node in the list becomes the new head of
- * the list.
+ * memory.
*
* @param list The lsit to operate on.
- * @param free_contents Determines whether to free the contents pointer.
*
* @return Pointer to the newly added node.
**/
-sll_node_t* sll_pop_front( sll_t* list, int free_contents );
+sll_node_t* sll_pop_front( sll_t* list );
/**
* @brief Removes and returns a pointer to the last element of the list.
*
* This function removes the last node from the list and frees it's associated
- * memory. If free_contents is passed a non-zero value then it's contents
- * pointer is also freed. The second to last node in the list becomes the new
- * tail of the list.
+ * memory.
*
* @param list The list to operate on.
- * @param free_contents Determines whether to free the contents pointer.
*
* @return Pointer to the newly added node.
**/
-sll_node_t* sll_pop_back( sll_t* list, int free_contents );
+sll_node_t* sll_pop_back( sll_t* list );
/**
* @brief Inserts a new node in a linked list at the specified index.
CHECK( (void*)0x1234 == node->contents );
CHECK( NULL != node->next );
CHECK( node == list.head );
- CHECK( node == list.tail );
+ CHECK( node != list.tail );
}
//-------------------------------------------------------------------------
{
sll_node_t node1 = { NULL, NULL };
sll_t list = { &node1, &node1 };
- sll_node_t* node = sll_push_front( &list, (void*)0x1234 );
+ sll_node_t* node = sll_push_back( &list, (void*)0x1234 );
CHECK( NULL != node );
CHECK( (void*)0x1234 == node->contents );
- CHECK( NULL != node->next );
- CHECK( node == list.head );
+ CHECK( &node1 != node->next );
+ CHECK( node != list.head );
CHECK( node == list.tail );
}
//-------------------------------------------------------------------------
// 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 };
+ CHECK( NULL == sll_pop_front( &list ) );
+ }
+
+ TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_1)
+ {
+ sll_node_t node1 = { NULL, NULL };
+ sll_t list = { &node1, &node1 };
+ CHECK( &node1 == sll_pop_front( &list ) );
+ CHECK( NULL == list.head );
+ CHECK( NULL == list.tail );
+ }
+
+ TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_2)
+ {
+ sll_node_t node2 = { NULL, NULL };
+ sll_node_t node1 = { NULL, &node2 };
+ sll_t list = { &node1, &node2 };
+ CHECK( &node1 == sll_pop_front( &list ) );
+ CHECK( &node2 == list.head );
+ CHECK( &node2 == list.tail );
+ }
+
//-------------------------------------------------------------------------
// 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 };
+ CHECK( NULL == sll_pop_back( &list ) );
+ }
+
+ TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1)
+ {
+ CHECK(false);
+ }
+
+ TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2)
+ {
+ CHECK(false);
+ }
+
//-------------------------------------------------------------------------
// Test sll_insert function
//-------------------------------------------------------------------------