Skip to content

Commit

Permalink
Merge pull request #214 from jean553/integrate_drop_at_double_linked_…
Browse files Browse the repository at this point in the history
…list

Integrate drop at double linked list
  • Loading branch information
jean553 authored Feb 4, 2018
2 parents 0970fa0 + a125a98 commit 059be7f
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
74 changes: 74 additions & 0 deletions double_linked_list/double_linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,77 @@ void insertAfter(DoubleLinkedList* list, const unsigned int index, const int dat

list->size++;
}

/**
*
*/
void dropAt(
DoubleLinkedList* list,
const unsigned int index
) {

DoubleLinkedListNode* node = list->head;
DoubleLinkedListNode* last = list->tail;

if (
index == 0 &&
node->next == NULL
) {
free(list->head);
list->head = NULL;
return;
}

if (index == 0) {
node->next->previous = NULL;
list->head = node->next;
free(node);
list->size -= 1;
return;
}

if (index == size(list) - 1) {
DoubleLinkedListNode* previousNode = last->previous;
previousNode->next = NULL;
list->tail = previousNode;
list->size -= 1;
free(last);

return;
}

const unsigned int length = size(list);
const unsigned int middle = length / 2;

if (index < middle)
{
node = list->head;

for (
unsigned int i = 0;
i != index;
i += 1
)
{
node = node->next;
}
}
else
{
node = list->tail;

for (
unsigned int i = length - 1;
i != index;
i -= 1
)
{
node = node->previous;
}
}

node->previous->next = node->next;
node->next->previous = node->previous;
free(node);
list->size -= 1;
}
11 changes: 11 additions & 0 deletions double_linked_list/double_linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ int* all(DoubleLinkedList* list);
*/
void insertAfter(DoubleLinkedList* list, const unsigned int index, const int data);

/**
* @brief Drops one node at the given index
*
* @param list the double linked list to modify
* @param index the position of the new node to remove
*/
void dropAt(
DoubleLinkedList* list,
const unsigned int index
);

#ifdef __cplusplus
}
#endif
136 changes: 136 additions & 0 deletions tests_double_linked_list/test_double_linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,141 @@ START_TEST(test_insertAt)
}
END_TEST

/**
*
*/
START_TEST(test_dropAt)
{
/* drop when only one node */

DoubleLinkedList first_list = create(0);
ck_assert_int_ne(first_list.head, NULL);

dropAt(&first_list, 0);

ck_assert_int_eq(first_list.head, NULL);

/* drop at the beginning when two items */

DoubleLinkedList second_list = create(10);
insertAtTheEnd(&second_list, 20);

ck_assert_int_ne(second_list.head, NULL);
ck_assert_int_ne(second_list.head->next, NULL);
ck_assert_int_ne(second_list.head->next->previous, NULL);
ck_assert_int_eq(at(&second_list, 0), 10);
ck_assert_int_eq(at(&second_list, 1), 20);

dropAt(&second_list, 0);

ck_assert_int_eq(at(&second_list, 0), 20);
ck_assert_int_ne(second_list.head, NULL);
ck_assert_int_eq(second_list.head->next, NULL);

/* drop at the end when two items */

DoubleLinkedList third_list = create(10);
insertAtTheEnd(&third_list, 20);

ck_assert_int_ne(third_list.head, NULL);
ck_assert_int_ne(third_list.head->next, NULL);
ck_assert_int_ne(third_list.head->next->previous, NULL);
ck_assert_int_eq(at(&third_list, 0), 10);
ck_assert_int_eq(at(&third_list, 1), 20);

dropAt(&third_list, 1);

ck_assert_int_eq(at(&third_list, 0), 10);
ck_assert_int_ne(third_list.head, NULL);
ck_assert_int_eq(third_list.head->next, NULL);

/* drop at the beginning when three items */

DoubleLinkedList fourth_list = create(10);
insertAtTheEnd(&fourth_list, 20);
insertAtTheEnd(&fourth_list, 30);

ck_assert_int_ne(fourth_list.head, NULL);
ck_assert_int_ne(fourth_list.head->next, NULL);
ck_assert_int_ne(fourth_list.head->next->previous, NULL);
ck_assert_int_ne(fourth_list.head->next->next->previous, NULL);
ck_assert_int_eq(at(&fourth_list, 0), 10);
ck_assert_int_eq(at(&fourth_list, 1), 20);
ck_assert_int_eq(at(&fourth_list, 2), 30);

dropAt(&fourth_list, 0);

ck_assert_int_eq(at(&fourth_list, 0), 20);
ck_assert_int_eq(at(&fourth_list, 1), 30);
ck_assert_int_ne(fourth_list.head, NULL);
ck_assert_int_ne(fourth_list.head->next, NULL);
ck_assert_int_eq(fourth_list.head->next->previous, fourth_list.head);
ck_assert_int_eq(fourth_list.head->next->next, NULL);

/* drop at the end when three items */

DoubleLinkedList fifth_list = create(10);
insertAtTheEnd(&fifth_list, 20);
insertAtTheEnd(&fifth_list, 30);

ck_assert_int_ne(fifth_list.head, NULL);
ck_assert_int_ne(fifth_list.head->next, NULL);
ck_assert_int_ne(fifth_list.head->next->previous, NULL);
ck_assert_int_ne(fifth_list.head->next->next->previous, NULL);
ck_assert_int_eq(at(&fifth_list, 0), 10);
ck_assert_int_eq(at(&fifth_list, 1), 20);
ck_assert_int_eq(at(&fifth_list, 2), 30);

dropAt(&fifth_list, 2);

ck_assert_int_eq(at(&fifth_list, 0), 10);
ck_assert_int_eq(at(&fifth_list, 1), 20);
ck_assert_int_eq(fifth_list.tail, fifth_list.head->next);

/* drop at the beginning when three items */

DoubleLinkedList sixth_list = create(10);
insertAtTheEnd(&sixth_list, 20);
insertAtTheEnd(&sixth_list, 30);

ck_assert_int_ne(sixth_list.head, NULL);
ck_assert_int_ne(sixth_list.head->next, NULL);
ck_assert_int_ne(sixth_list.head->next->previous, NULL);
ck_assert_int_ne(sixth_list.head->next->next->previous, NULL);
ck_assert_int_eq(at(&sixth_list, 0), 10);
ck_assert_int_eq(at(&sixth_list, 1), 20);
ck_assert_int_eq(at(&sixth_list, 2), 30);

dropAt(&sixth_list, 1);

ck_assert_int_eq(at(&sixth_list, 0), 10);
ck_assert_int_eq(at(&sixth_list, 1), 30);
ck_assert_int_eq(sixth_list.tail->previous, sixth_list.head);
ck_assert_int_eq(sixth_list.head->next, sixth_list.tail);

/* multiple drops from the head and the tail */

DoubleLinkedList seventh_list = create(10);
insertAtTheEnd(&seventh_list, 20);
insertAtTheEnd(&seventh_list, 30);
insertAtTheEnd(&seventh_list, 40);
insertAtTheEnd(&seventh_list, 50);

ck_assert_int_eq(at(&seventh_list, 0), 10);
ck_assert_int_eq(at(&seventh_list, 1), 20);
ck_assert_int_eq(at(&seventh_list, 2), 30);
ck_assert_int_eq(at(&seventh_list, 3), 40);
ck_assert_int_eq(at(&seventh_list, 4), 50);

dropAt(&seventh_list, 1);
dropAt(&seventh_list, 2);

ck_assert_int_eq(at(&seventh_list, 0), 10);
ck_assert_int_eq(at(&seventh_list, 1), 30);
ck_assert_int_eq(at(&seventh_list, 2), 50);
}
END_TEST

/**
*
*/
Expand All @@ -193,6 +328,7 @@ Suite* double_linked_list_suite()
tcase_add_test(tcase, test_size);
tcase_add_test(tcase, test_at);
tcase_add_test(tcase, test_insertAt);
tcase_add_test(tcase, test_dropAt);

suite_add_tcase(suite, tcase);

Expand Down

0 comments on commit 059be7f

Please sign in to comment.