]> git.mdlowis.com Git - projs/libcds.git/commitdiff
Added more tests for singly linked list
authorMike D. Lowis <mike@mdlowis.com>
Tue, 5 Jun 2012 16:02:50 +0000 (12:02 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Tue, 5 Jun 2012 16:02:50 +0000 (12:02 -0400)
source/lists/single_link/sll.c
source/lists/single_link/sll.h
tests/test_sll.cpp

index a8940c55f53514e8ef2f554205253694a3278d96..c27d04fa313f061d68e12d440b2b00bf083ee133 100644 (file)
@@ -44,6 +44,26 @@ void sll_free_node(sll_node_t* node, int free_contents)
     }
 }
 
+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;
@@ -135,19 +155,61 @@ sll_node_t* sll_pop_front( sll_t* list )
 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;
 }
 
index 65d5d9594e0060fce1b0bc6315e417bf42893ebe..d937aa0d0573cb02f73b69c6368024c4e8ee937b 100644 (file)
@@ -57,6 +57,9 @@ void sll_free(sll_t* list, int free_contents);
  */
 void sll_free_node(sll_node_t* node, int free_contents);
 
+sll_node_t* sll_front( sll_t* list );
+sll_node_t* sll_back( sll_t* list );
+
 /**
  * @brief Returns the number of elements in the list.
  *
index 6a91b96c101ca8069b0d6c4749e246461aef5b12..4ac71a34fa98901c10832ab88e42570d65845ee6 100644 (file)
@@ -88,6 +88,50 @@ namespace {
         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
     //-------------------------------------------------------------------------
@@ -265,6 +309,17 @@ namespace {
         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
     //-------------------------------------------------------------------------
@@ -281,12 +336,32 @@ namespace {
 
     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 );
     }
 
     //-------------------------------------------------------------------------