Question About Linked Lists Example
I've been working my way through some examples about how to program C, and I think I'm beginning to understand the basics of linked lists. In the following tutorial, they use some temporary variables to work their way through a linked list.
However, the full code isn't shown, and the link to the example code doesn't work. So I have a question.
Before this code, they must have declared and allocated memory for "current_node" first, right?
When I made a program based on the example, I did the following first:
// First deal with the base case, setting the head pointer
head = (struct list_node *) malloc(sizeof(struct list_node));
head->node_id = 1;
// Set a temp variable to our current last node
current_node = head;
Was I right to do this? It seems like I had to. Similarly, at the bottom of the tutorial, they do this:
struct person *current_node;
current_node = (struct person *) malloc(sizeof(struct person)); // allocate memory
Again, you would need to do something like this first, right?
// Copy the head pointer
temp = head;
struct person *temp;
temp = (struct person *) malloc(sizeof(struct person)); // allocate memory