I'm stuck on linked lists. I'm confused as to how to create a new linked list --- there are loads of examples on the internet of how to iterate through a linked list that ALREADY exists, and there are examples of how to create a linked list manually with separately named nodes, or how to insert a node into a list that already exists. However, for the purposes of my homework (which involves a MUCH more complicated task than simply implementing a linked list), I'm going to need to iterate through some data, creating a linked list from it. Actually, I'm going to have to create a hash table, but BABY STEPS!
What I would LOVE is if someone gave me a simple, dummy explanation of how to implement a linked list of 1, 2, 3.
For my purposes, I am assuming that a struct node is
Okay? From what I've gathered, the head node contains no value, and is declared and allocated to point to the next node as such:Code:typedef struct node { int i; struct node* next; } node;
Soo to point it to the next node... We do something like this?Code:node* head = malloc(sizeof(node));
And we'd have to allocate that as well, and give it a valueCode:head->next = node* new;
[/CODE]Code:new = malloc(sizeof(node)); new->i = 1;
Okay, good. Have I successfully made a list of 1? That's about as far as I can get. Then I want to link the node new to the next node.
??????Code:new->next = new;
Here is where I get lost. Suppose that I'm actually doing this in a for loop, so I want to be able to do this 3 times. This doesn't seem right to me??
Can someone please help and show me a simple, bare bones way to do this? Also, please let me know if I have gotten ANY of this right. I am so confused!
I am NOT asking anyone to do my homework for me. This isn't even what my assignment is, but I can't do my assignment without grasping this concept.