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
This commit is contained in:
Brian Barto 2016-04-07 14:49:47 -04:00
parent 420987a544
commit a175b434ed
1 changed files with 15 additions and 20 deletions

View File

@ -19,9 +19,9 @@ int main(void) {
int col; int col;
struct winpos *next; struct winpos *next;
}; };
struct winpos start; struct winpos *list_pointer = NULL;
struct winpos *list_pointer = &start; struct winpos *start; // Always points to start of list
struct winpos *temp; // Used for free()ing the list struct winpos *temp; // Used for free()ing the list
int termSizeRows = getTermSizeRows(); int termSizeRows = getTermSizeRows();
int termSizeCols = getTermSizeCols(); int termSizeCols = getTermSizeCols();
@ -34,21 +34,16 @@ int main(void) {
x = 1; x = 1;
} else if (isspace(c)) { } else if (isspace(c)) {
++x; ++x;
} else if (first) {
start.source = c;
start.row = y;
start.col = x;
start.next = (struct winpos *) 0;
first = false;
++x;
} else { } else {
// Allocate space for the new struct in our linked list if (first) {
// and point *next to it. list_pointer = malloc(sizeof(struct winpos));
list_pointer->next = 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->source = c;
list_pointer->row = y; list_pointer->row = y;
list_pointer->col = x; list_pointer->col = x;
@ -60,7 +55,8 @@ int main(void) {
clearTermWindow(termSizeRows, termSizeCols); clearTermWindow(termSizeRows, termSizeCols);
list_pointer = &start; // Printing the list
list_pointer = start;
while (list_pointer != (struct winpos *) 0) { while (list_pointer != (struct winpos *) 0) {
printf("row: %i, ", list_pointer->row); printf("row: %i, ", list_pointer->row);
printf("col: %i, ", list_pointer->col); printf("col: %i, ", list_pointer->col);
@ -68,9 +64,8 @@ int main(void) {
list_pointer = list_pointer->next; list_pointer = list_pointer->next;
} }
// Freeing the list. Starting with the second list item because // Freeing the list.
// the first one was not created with malloc list_pointer = start;
list_pointer = start.next;
while (list_pointer != (struct winpos *) 0) { while (list_pointer != (struct winpos *) 0) {
temp = list_pointer; temp = list_pointer;
list_pointer = list_pointer->next; list_pointer = list_pointer->next;