Hey all,
I'm getting myself in a bit of a mess I think. Let's say you've set up 2 pointers to structs, for a linked list. You've initialised them both to NULL, as in the following code fragment:
Code:
typedef struct something {
int key;
struct something *next;
} somestuff;
somestuff *topnode = NULL, *currentnode = NULL;
int main(void) {
...
}
My question is, is it advisable to make a call to malloc() to use topnode and currentnode? I usually end up adding nodes with something like:
Code:
currentnode = topnode;
if (topnode == NULL)
topnode = newnode;
else
{
for (currentnode = topnode; currentnode->next != NULL; currentnode = currentnode->next)
;
currentnode->next = newnode;
}
Just getting a bit confused as to when to use it. Could do with a simple explanation (or link to one if possible. I've looked but Google is coming up with some weird links to parsing with malloc()).
Be very grateful for any help you can provide me.
--John.