c - Avoiding Extra Malloc in Linked List (node->next = NULL) -
in linked list, i'm trying avoid malloc
ing node without adding bunch of if
statements , such. have following:
polynomial create() { polynomial head = null; polynomial temp = null; int numterms, coefficient, exponent; int counter = 0; printf("enter number of terms in polynomial: "); scanf ("%d", &numterms); printf("\n"); if (numterms == 0) { head = malloc(sizeof(term)); head->coef = 0; head->exp = 0; head->next = null; return head; } while (numterms != counter) { // ask input printf("enter coefficient , exponent of term %d: ", (counter + 1)); scanf("%d %d", &coefficient, &exponent); printf("\n"); // create term if (temp == null) temp = malloc(sizeof(term)); temp->coef = coefficient; temp->exp = exponent; temp->next = null; //if((numterms - 1) != counter) temp->next = malloc(sizeof(term)); -- workaround printf("created: %d %d\n", temp->coef, temp->exp); // if first node created, mark head if (counter == 0) head = temp; // increment list , counter temp = temp->next; counter ++; } return head; }
but when go print polynomial (i have function works so), following:
polynomial 1: 3x^4
--> in case, reference head->next null
so tried following workaround - allocate memory in advance new nodes, if last iteration of user input. accomplished by:
replace temp->next = null;
with
if((numterms - 1) != counter) temp->next = malloc(sizeof(term));
the numterms - 1
prevents adding 'extra node', , malloc keep reference temp->next alive. if don't use if
check , always allocate memory, end following:
polynomial 1: 3x^4 - 7x^2 + 5 + 10621224x^10617028
what part of allocation missing causes reference temp->next lost? i'm really terrible pointers , memory management in general, terrible question.
you're making harder needs be. consider simple node-population linked list conforms following:
- assumes null means empty list
- allocates head pointer without having test each allocation.
- one, , only one allocation per-node required.
- nodes presented in list in entry-order. first node in list first 1 entered, second node second entered, etc.
with that, see below general algorithm how adapts code:
struct node { // fields here struct node *next; }; struct node* populate_list() { struct node *head = null; struct node **pp = &head; int i=0, count=0; // todo: set count: insertion limit here. // run insertion loop (i=0; i<count; ++i) { struct node *p = malloc(sizeof(*p)); // todo: initialize node members here // save our current tail-pointer, // head pointer. advance new tail // pointer , continue loop *pp = p; pp = &p->next; } // terminate list. *pp = null; // , return head pointer. return head; }
note: p
there clarity. can reduce loop body following, totally valid:
// run insertion loop (i=0; i<count; ++i) { *pp = malloc(sizeof(**pp)); // todo: initialize node members here // using (*pp)->member access // save next pointer , continue. pp = &(*pp)->next; }
adapting code
now know how this, considerably reduce code this:
polynomial create() { polynomial head = null; polynomial *pp = &head; int numterms, coefficient, exponent; int counter = 0; // prompt valid number of terms. printf("enter number of terms in polynomial: "); scanf ("%d", &numterms); while (numterms != counter) { // ask input printf("enter coefficient , exponent of term %d: ", (counter + 1)); if (scanf("%d %d", &coefficient, &exponent) == 2) { *pp = malloc(sizeof(**pp)); (*pp)->coef = coefficient; (*pp)->exp = exponent; pp = &(*pp)->next; ++counter; } else { // eat line , try again. scanf("%*[^\n]\n") } } *pp = null; return head; }
Comments
Post a Comment