Skip to content

Commit

Permalink
Add list reverse and length functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeraymond committed Apr 6, 2012
1 parent 54324a3 commit 57fa71e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/np_linkedlist.c
Expand Up @@ -93,3 +93,43 @@ void *np_linkedlist_pop(struct NpLinkedList *list)
free(node);
return item;
}

/**
Reverses the list.
@param list The list.
*/
void np_linkedlist_reverse(struct NpLinkedList *list)
{
struct NpLinkedListNode *prev;
struct NpLinkedListNode *next;

prev = NULL;
while(list->head) {
next = list->head->next;
list->head->next = prev;
prev = list->head;
list->head = next;
}
list->head = prev;
}

/**
Determines the length of the list.
@param list The list.
@return the length of the list.
*/
unsigned np_linkedlist_length(struct NpLinkedList *list)
{
struct NpLinkedListNode *node;
unsigned length;

length = 0;
node = list->head;
while (node) {
++length;
node = node->next;
}
return length;
}
2 changes: 2 additions & 0 deletions src/np_linkedlist.h
Expand Up @@ -32,5 +32,7 @@ struct NpLinkedList *np_linkedlist_new();
void np_linkedlist_free(struct NpLinkedList *list);
void *np_linkedlist_push(struct NpLinkedList *list, void *item);
void *np_linkedlist_pop(struct NpLinkedList *list);
void np_linkedlist_reverse(struct NpLinkedList *list);
unsigned np_linkedlist_length(struct NpLinkedList *list);

#endif
31 changes: 28 additions & 3 deletions test/np_linkedlist_test.c
Expand Up @@ -27,14 +27,39 @@ void np_linkedlist_test(void) {
char *data1 = "The first data item";
char *data2 = "The second data item";

/* push and pop */
CU_ASSERT_NOT_EQUAL(NULL, list = np_linkedlist_new());
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_push(list, data1));
CU_ASSERT_STRING_EQUAL(data1, (char *)np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_pop(list));
CU_ASSERT_EQUAL(NULL, np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_push(list, data1));
CU_ASSERT_STRING_EQUAL(data2, np_linkedlist_push(list, data2));
CU_ASSERT_STRING_EQUAL(data2, (char *)np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data1, (char *)np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data2, np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_pop(list));
CU_ASSERT_EQUAL(NULL, np_linkedlist_pop(list));

/* reverse */
np_linkedlist_reverse(list);
CU_ASSERT_EQUAL(NULL, np_linkedlist_pop(list));

np_linkedlist_push(list, data1);
np_linkedlist_reverse(list);
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_pop(list));
CU_ASSERT_EQUAL(NULL, np_linkedlist_pop(list));

np_linkedlist_push(list, data1);
np_linkedlist_push(list, data2);
np_linkedlist_reverse(list);
CU_ASSERT_STRING_EQUAL(data1, np_linkedlist_pop(list));
CU_ASSERT_STRING_EQUAL(data2, np_linkedlist_pop(list));
CU_ASSERT_EQUAL(NULL, np_linkedlist_pop(list));

/* length */
CU_ASSERT_EQUAL(0, np_linkedlist_length(list));
np_linkedlist_push(list, data1);
CU_ASSERT_EQUAL(1, np_linkedlist_length(list));
np_linkedlist_push(list, data1);
CU_ASSERT_EQUAL(2, np_linkedlist_length(list));

np_linkedlist_free(list);
}

0 comments on commit 57fa71e

Please sign in to comment.