1. ## Linked Lists Basic Help Needed

Hey guys, I'm trying to learn/understand linked lists so I wrote this program just to see if I could do it, and to try and understand the process better etc. It compiles fine but when I read it I get memory access violation errors and stuff. Could someone please tell me what's going on. Basically I just wanted it to print 1-29 or whatever, and it would create 30 structure nodes, and each printed digit would be from each seperate node. Here is the code!

Code:
```#include <iostream>

int main()
{
int i = 0;
struct node {
int x;
node *next;
};
node *root;
node *conductor;
root = new node;
root->x = i;
root->next = new node;
conductor = root->next;
if (conductor != 0)
{
while (conductor != 0 && i < 30)
{
conductor->x = i++;
std::cout<<conductor->x;
conductor = conductor->next;
}
}
std::cin.get();
return 0;
}```

2. 1. You need a 'new' inside the loop, for each node that you add.
2. You need to append each node to the end of the list (and update your end of list pointer).

So
- A pointer to the head (initially NULL). This is assigned ONCE when you create the first node.
- A pointer to the tail (initially NULL). This is updated whenever you append a node.

Also, vitally important to to make sure that tail->next is always NULL.

3. Originally Posted by Salem
1. You need a 'new' inside the loop, for each node that you add.
2. You need to append each node to the end of the list (and update your end of list pointer).

So
- A pointer to the head (initially NULL). This is assigned ONCE when you create the first node.
- A pointer to the tail (initially NULL). This is updated whenever you append a node.

Also, vitally important to to make sure that tail->next is always NULL.

Isn't that the node *root I have? Or am I supposed to create another pointer that points to the first node aswell? And with the "A pointer to the tail" how do I know where the tail is going to be, aka how many nodes there is going to be? Hmmm.

4. The whole point of linked lists is that you've no idea in advance how many you need.
So they're allocated on demand (ie, INSIDE your loop), and chained together (aka LINKED).

The ->next pointer of the old node points to the new node, and so on as more node are added.

5. yeah I get you you said "A pointer to the tail" though, so didn't you mean a pointer to the end node? If so, how do you know where that is :O did you mean create a pointer then assign it to the end node once you know how many there is or?

6. No, I mean something like this

tail->next = newNode; // add a node to the end of the list
tail = newNode; // now it's the end of the list

You can call it whatever you want, I just call them "head" and "tail".