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:
parent
420987a544
commit
a175b434ed
31
src/nms.c
31
src/nms.c
@ -19,8 +19,8 @@ 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));
|
||||||
|
start = list_pointer;
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
list_pointer->next = malloc(sizeof(struct winpos));
|
list_pointer->next = malloc(sizeof(struct winpos));
|
||||||
|
|
||||||
// Now let's point list_pointer to the next structure
|
|
||||||
// and populate it.
|
|
||||||
list_pointer = list_pointer->next;
|
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user