Skip to content

Commit

Permalink
Reimplementing slist_reverse to fix a clang warning and updating slis…
Browse files Browse the repository at this point in the history
…t_reverse tests
  • Loading branch information
srdja committed Apr 6, 2016
1 parent 1ee59d8 commit 93b5152
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
9 changes: 3 additions & 6 deletions src/slist.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,20 +734,17 @@ void slist_reverse(SList *list)
return;

SNode *prev = NULL;
SNode *next = NULL;
SNode *flip = list->head;
SNode *next = flip->next;

list->tail = list->head;

size_t i;
for (i = 0; i < list->size; i++) {
while (flip) {
next = flip->next;
flip->next = prev;

prev = flip;
flip = next;
next = next ? next->next : NULL;
}

list->head = prev;
}

Expand Down
15 changes: 10 additions & 5 deletions test/slist_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,13 +1027,18 @@ void test_slist_reverse()
SList *l;
slist_1_to_10(&l);

slist_foreach(l, p);
printf("\n");

slist_reverse(l);

slist_foreach(l, p);
printf("\n");
SListIter i;
slist_iter_init(&i, l);

void *e;
int next = 10;
while (slist_iter_next(&i, &e) != CC_ITER_END) {
cc_assert(next == *((int*) e),
cc_msg("slist_reverse: Expected %d, but got %d instead", next, *((int*) e)));
next--;
}
}

void slist_1234(SList **out)
Expand Down

0 comments on commit 93b5152

Please sign in to comment.