return ((NULL == list->head) && (NULL == list->tail));
}
+list_node_t* list_prev(list_t* list, list_node_t* node){
+ list_node_t* prev = (NULL != list && NULL != node && list->head != node) ? list->head : NULL;
+ while(NULL != prev && prev->next != node) prev = prev->next;
+ return prev;
+}
+
list_node_t* list_at(list_t* list, size_t index)
{
list_node_t* node = NULL;
return node;
}
-#include <stdio.h>
-
list_node_t* list_push_front( list_t* list, void* contents )
{
list_node_t* node = list_new_node( contents );
*/
bool list_empty(list_t* list);
+/**
+ * @brief Find the node before the given one in the specified list.
+ *
+ * @param list The list to search thru
+ * @param node The node to search for
+ *
+ * @return Pointer to the node before the given node or
+ * NULL if given node is NULL, not present, or is the head of list
+ */
+list_node_t* list_prev(list_t* list, list_node_t* node);
+
/**
* @brief Return the node at the specified index in a linked list.
*
CHECK( 0 == list_empty( &list ) );
}
+ //-------------------------------------------------------------------------
+ // Test list_prev function
+ //-------------------------------------------------------------------------
+ TEST(Verify_list_prev_returns_NULL_when_list_is_empty)
+ {
+ list_t list = { NULL, NULL };
+ list_node_t bogus = { NULL, NULL };
+ CHECK( NULL == list_prev(&list, NULL) );
+ CHECK( NULL == list_prev(&list, &bogus) );
+ }
+
+ TEST(Verify_list_prev_returns_NULL_when_given_NULL)
+ {
+ list_node_t node3 = { NULL, NULL };
+ list_node_t node2 = { NULL, &node3 };
+ list_node_t node1 = { NULL, &node2 };
+ list_t list = { &node1, &node3 };
+ CHECK( NULL == list_prev(&list, NULL) );
+ }
+
+ TEST(Verify_list_prev_returns_NULL_when_given_node_not_present)
+ {
+ list_node_t node3 = { NULL, NULL };
+ list_node_t node2 = { NULL, &node3 };
+ list_node_t node1 = { NULL, &node2 };
+ list_node_t bogus = { NULL, &node2 };
+ list_t list = { &node1, &node3 };
+ CHECK( NULL == list_prev(&list, &bogus) );
+ }
+
+ TEST(Verify_list_prev_returns_NULL_when_given_node_at_head)
+ {
+ list_node_t node3 = { NULL, NULL };
+ list_node_t node2 = { NULL, &node3 };
+ list_node_t node1 = { NULL, &node2 };
+ list_t list = { &node1, &node3 };
+ CHECK( NULL == list_prev(&list, &node1) );
+ }
+
+ TEST(Verify_list_prev_returns_previous_node)
+ {
+ list_node_t node4 = { NULL, NULL };
+ list_node_t node3 = { NULL, &node4 };
+ list_node_t node2 = { NULL, &node3 };
+ list_node_t node1 = { NULL, &node2 };
+ list_t list = { &node1, &node3 };
+ CHECK( &node1 == list_prev(&list, &node2) );
+ CHECK( &node2 == list_prev(&list, &node3) );
+ CHECK( &node3 == list_prev(&list, &node4) );
+ }
+
//-------------------------------------------------------------------------
// Test list_at function
//-------------------------------------------------------------------------