Skip to content

Commit

Permalink
updating exercise 33 lcthw
Browse files Browse the repository at this point in the history
  • Loading branch information
nkane committed Nov 17, 2018
1 parent 7a575ed commit e78d779
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion c-the-hard-way/exercises/33/liblcthw/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
CC=clang
#CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic $(OPTFLAGS)

LIBS=-ldl $(OPTLIBS)
PREFIX?=/usr/local
Expand Down
2 changes: 1 addition & 1 deletion c-the-hard-way/exercises/33/liblcthw/src/lcthw/dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n",\
#define debug(M, ...) fprintf(stderr, "[DEBUG] %s:%d: " M "\n",\
__FILE__, __LINE__, ##__VA_ARGS__)
#endif

Expand Down
3 changes: 2 additions & 1 deletion c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ List_Remove(List * list, ListNode * node)
}

void
List_Print(List * list)
List_Print(List * list, const char *message)
{
check(list, "List is NULL");
log_info("%s", message);
LIST_FOREACH(list, first, next, current)
{
log_info("Node [%p] - value: %s -- prev [%p] <- * -> next [%p]", current, (char *)current->value, current->prev, current->next);
Expand Down
2 changes: 1 addition & 1 deletion c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void *
List_Remove(List * list, ListNode * node);

void
List_Print(List * list);
List_Print(List * list, const char *);

#define LIST_FOREACH(L, S, M, V) ListNode *_node = NULL;\
ListNode *V = NULL;\
Expand Down
18 changes: 14 additions & 4 deletions c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,39 @@ ListNode_Swap(ListNode *n1, ListNode *n2)
int
List_bubble_sort(List *list, List_compare list_compare_function)
{
int result = -1;
if (list && list_compare_function)
{
ListNode *node_one = list->first;
ListNode *node_two = node_one->next;

int i;
int j;
for (i = 0; i < list->count; i++)
{
node_one = list->first;
node_two = node_one->next;
for (j = 0; j < list->count - i - 1; j++)
{
if (list_compare_function(node_one, node_two))
// check if node_one value is greater than node_two value
if (list_compare_function(node_one, node_two) == 1)
{
//swap(node_one, node_two);
ListNode_Swap(node_one, node_two);
}
node_one = node_two;
node_two = node_one->next;
}
}
result = 0;
}
return -1;
return result;
}

List *
List_merge_sort(List *list, List_compare list_compare_function)
{
if (list && list_compare_function)
{
return list;
}
return NULL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ test_bubble_sort()
{
List *words = create_words();

List_Print(words);
List_Print(words, "Before Bubble Sort");

// should work on a list that needs sorting
int rc = List_bubble_sort(words, (List_compare) strcmp);

List_Print(words, "After Bubble Sort");

mu_assert(rc == 0, "Bubble sort failed.");
mu_assert(is_sorted(words), "Words are not sorted after bubble sort.");

Expand Down
1 change: 0 additions & 1 deletion c-the-hard-way/exercises/33/liblcthw/tests/minunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
message = test(); tests_run++; if (message) return message;

#define RUN_TESTS(name) int main(int argc, char *argv[]) {\
argc = 1; \
debug("----- RUNNING: %s", argv[0]);\
printf("-----\nRUNNING: %s\n", argv[0]);\
char *result = name();\
Expand Down

0 comments on commit e78d779

Please sign in to comment.