From a175b434ed47d1b56bc3eee4b0f884c6a8489a35 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Thu, 7 Apr 2016 14:49:47 -0400 Subject: [PATCH] Cleaned up the list building process a bit. Made it so all elements are dynamically allocated, instead of all but the first like before this commit. This code is a bit smoother and easier to follow. modified: src/nms.c --- src/nms.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/nms.c b/src/nms.c index 8212c3d..45932f3 100644 --- a/src/nms.c +++ b/src/nms.c @@ -19,9 +19,9 @@ int main(void) { int col; struct winpos *next; }; - struct winpos start; - struct winpos *list_pointer = &start; - struct winpos *temp; // Used for free()ing the list + struct winpos *list_pointer = NULL; + struct winpos *start; // Always points to start of list + struct winpos *temp; // Used for free()ing the list int termSizeRows = getTermSizeRows(); int termSizeCols = getTermSizeCols(); @@ -34,21 +34,16 @@ int main(void) { x = 1; } else if (isspace(c)) { ++x; - } else if (first) { - start.source = c; - start.row = y; - start.col = x; - start.next = (struct winpos *) 0; - first = false; - ++x; } else { - // Allocate space for the new struct in our linked list - // and point *next to it. - list_pointer->next = malloc(sizeof(struct winpos)); + if (first) { + list_pointer = malloc(sizeof(struct winpos)); + start = list_pointer; + first = false; + } else { + list_pointer->next = malloc(sizeof(struct winpos)); + list_pointer = list_pointer->next; + } - // Now let's point list_pointer to the next structure - // and populate it. - list_pointer = list_pointer->next; list_pointer->source = c; list_pointer->row = y; list_pointer->col = x; @@ -60,7 +55,8 @@ int main(void) { clearTermWindow(termSizeRows, termSizeCols); - list_pointer = &start; + // Printing the list + list_pointer = start; while (list_pointer != (struct winpos *) 0) { printf("row: %i, ", list_pointer->row); printf("col: %i, ", list_pointer->col); @@ -68,9 +64,8 @@ int main(void) { list_pointer = list_pointer->next; } - // Freeing the list. Starting with the second list item because - // the first one was not created with malloc - list_pointer = start.next; + // Freeing the list. + list_pointer = start; while (list_pointer != (struct winpos *) 0) { temp = list_pointer; list_pointer = list_pointer->next;