From e568c588249616004df3d59ae4808d65c8b8f5eb Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 10 Mar 2012 21:15:30 +0000 Subject: [PATCH] Ordered contact list --- contact_list.c | 17 ++++++++++++++++- test_contact_list.c | 39 ++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/contact_list.c b/contact_list.c index 3a44522d..8522fd94 100644 --- a/contact_list.c +++ b/contact_list.c @@ -100,12 +100,27 @@ int contact_list_add(const char * const name, const char * const show, while(curr) { contact_t *curr_contact = curr->contact; - if (strcmp(curr_contact->name, name) == 0) { + + // insert + if (strcmp(curr_contact->name, name) > 0) { + if (prev) { + struct _contact_node_t *new = _make_contact_node(name, show, status); + new->next = curr; + prev->next = new; + } else { + struct _contact_node_t *new = _make_contact_node(name, show, status); + new->next = _contact_list; + _contact_list = new; + } + return 0; + // update + } else if (strcmp(curr_contact->name, name) == 0) { _destroy_contact(curr->contact); curr->contact = _new_contact(name, show, status); return 0; } + // move on prev = curr; curr = curr->next; } diff --git a/test_contact_list.c b/test_contact_list.c index 35b6cb73..09460cdf 100644 --- a/test_contact_list.c +++ b/test_contact_list.c @@ -1,4 +1,5 @@ #include +#include #include #include "contact_list.h" @@ -43,8 +44,8 @@ static void first_and_second_elements_correct(void) contact_list_add("James", NULL, NULL); contact_list_add("Dave", NULL, NULL); contact_list_t *list = get_contact_list(); - contact_t *james = list->contacts[0]; - contact_t *dave = list->contacts[1]; + contact_t *dave = list->contacts[0]; + contact_t *james = list->contacts[1]; assert_string_equals("James", james->name); assert_string_equals("Dave", dave->name); @@ -53,8 +54,8 @@ static void first_and_second_elements_correct(void) static void contains_three_elements(void) { contact_list_add("James", NULL, NULL); - contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); + contact_list_add("Dave", NULL, NULL); contact_list_t *list = get_contact_list(); assert_int_equals(3, list->size); @@ -62,13 +63,13 @@ static void contains_three_elements(void) static void first_three_elements_correct(void) { - contact_list_add("James", NULL, NULL); - contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("James", NULL, NULL); contact_list_t *list = get_contact_list(); - contact_t *james = list->contacts[0]; + contact_t *bob = list->contacts[0]; contact_t *dave = list->contacts[1]; - contact_t *bob = list->contacts[2]; + contact_t *james = list->contacts[2]; assert_string_equals("James", james->name); assert_string_equals("Dave", dave->name); @@ -82,9 +83,9 @@ static void add_twice_at_beginning_adds_once(void) contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); contact_list_t *list = get_contact_list(); - contact_t *james = list->contacts[0]; + contact_t *bob = list->contacts[0]; contact_t *dave = list->contacts[1]; - contact_t *bob = list->contacts[2]; + contact_t *james = list->contacts[2]; assert_int_equals(3, list->size); assert_string_equals("James", james->name); @@ -99,9 +100,9 @@ static void add_twice_in_middle_adds_once(void) contact_list_add("James", NULL, NULL); contact_list_add("Bob", NULL, NULL); contact_list_t *list = get_contact_list(); - contact_t *james = list->contacts[0]; + contact_t *bob = list->contacts[0]; contact_t *dave = list->contacts[1]; - contact_t *bob = list->contacts[2]; + contact_t *james = list->contacts[2]; assert_int_equals(3, list->size); assert_string_equals("James", james->name); @@ -116,9 +117,9 @@ static void add_twice_at_end_adds_once(void) contact_list_add("Bob", NULL, NULL); contact_list_add("James", NULL, NULL); contact_list_t *list = get_contact_list(); - contact_t *james = list->contacts[0]; + contact_t *bob = list->contacts[0]; contact_t *dave = list->contacts[1]; - contact_t *bob = list->contacts[2]; + contact_t *james = list->contacts[2]; assert_int_equals(3, list->size); assert_string_equals("James", james->name); @@ -179,8 +180,8 @@ static void remove_first_when_three(void) contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - contact_t *dave = list->contacts[0]; - contact_t *bob = list->contacts[1]; + contact_t *bob = list->contacts[0]; + contact_t *dave = list->contacts[1]; assert_string_equals("Dave", dave->name); assert_string_equals("Bob", bob->name); @@ -196,8 +197,8 @@ static void remove_second_when_three(void) contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - contact_t *james = list->contacts[0]; - contact_t *bob = list->contacts[1]; + contact_t *bob = list->contacts[0]; + contact_t *james = list->contacts[1]; assert_string_equals("James", james->name); assert_string_equals("Bob", bob->name); @@ -213,8 +214,8 @@ static void remove_third_when_three(void) contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - contact_t *james = list->contacts[0]; - contact_t *dave = list->contacts[1]; + contact_t *dave = list->contacts[0]; + contact_t *james = list->contacts[1]; assert_string_equals("James", james->name); assert_string_equals("Dave", dave->name);