# how to add a note to the begining of linked list

• 10-24-2005
merixa
how to add a new node to the beginning of my linked list
Hi guys

I have been studying linked list tutorial. I want to add a new node to the begining of my linked list but I am making a mistake.

Can anyone show me what is wrong with my code?

Code:

#include <iostream>
using namespace std;

class node
{
public:
int number;
node* next;
};

int main()
{
node* root; //head point to first node
node* conductor; //pointer that moves node by node

root=new node; //dynamicly creating a new node and point to head
root->next=0;
root->number=5; //first number in my list

conductor=root; //set conductor to root

if (conductor!=0) //check if there is a node in the list
{
while(conductor->next!=0) //go to the end of the list
conductor=conductor->next;
}

conductor->next=new node; //create a new node
conductor=conductor->next; //set conductor to new node
conductor->number=10; //set seconds node number to 10
conductor->next=0;  //set node next pointer to the NULL

conductor->next=new node; //create third node to put it the beginning of the list
conductor->number=1; //set third number to 1
conductor->next=root; //set the next part of the new node equal to root, which makes it point to the first node in the list ????
conductor=root; //set root to point to the new node ???

if (conductor->next!=0) //print list
{
while(conductor->next!=0)
{
cout<<conductor->number<<' ';
conductor=conductor->next;
}

cout<<conductor->number;
}

cout<<endl;

Output should be like this

1 5 10

Thanks
• 10-24-2005
7stud
note != node

The best way to understand linked lists is to draw diagrams. Go through your code step by step and draw a box when you create a new node. Then draw an arrow from the pointer in a node to the other node it points to.

Adding a node to the beginning of a linked list should be easy:

1) Create the node.
2) Get a pointer to the first element
3) Assign the pointer in 2) to the new node's next member
• 10-24-2005
merixa
Quote:

Originally Posted by 7stud
note != node

The best way to understand linked lists is to draw diagrams. Go through your code step by step and draw a box when you create a new node. Then draw an arrow from the pointer in a node to the other node it points to.

thanks for replying but I already tried to draw diagrams. I got the idea but I can't put it on code. :confused: any way I like C++.. I will do it.

What I want to do for adding a new node to the beginning of my list is

1. Get a new node pointed to by conductor
set the number part of this node equal to 1
2. set the next part of the new node eqaul to root, which makes it point to the first node in the list.
3. set root to point to the new node

what u guys think??

p.s: I need another good tutorial any idea?

thanks
• 10-24-2005
7stud
Quote:

thanks for replying but I tried to draw diagram. I got the idea but I can't put it on code.

1) Create the first node.
2) Create a second node.
3) Assign the address of the second node to the first node's next pointer.

4) Create a third node.
5) Assign the address of the third node to the second node's next pointer.

Repeat.

The problem is: how do you get back to the first node? One way is to keep track of the address of the first node. Assign it to a pointer variable called begin if you want.
• 10-24-2005
merixa
Quote:

Originally Posted by 7stud

1) Create the first node.
2) Create a second node.
3) Assign the address of the second node to the first node's next pointer.

4) Create a third node.
5) Assign the address of the third node to the second node's next pointer.

if I follow these steps, I will create a linked list by adding a new node to the end of the list right? (I am asking to make sure that I got your point)

What I want to do is I want to create the first node and add another one to the end of first node than create third one and put it in front of the list..
• 10-24-2005
7stud
Quote:

What I want to do for adding a new node to the beginning of my list is

1. Get a new node pointed to by conductor
node* conductor;
node aNode;
conductor = &aNode;

Quote:

set the number part of this node equal to 1
conductor->num = 1;

Quote:

2. set the next part of the new node eqaul to root, which makes it point to the first node in the list.
conductor->next = root;

Quote:

3. set root to point to the new node
root = conductor;
• 10-24-2005
merixa
@7stud..

Thanks for your help.. I think I should sleep now beause I have been studying for 4 hours and I can't think any more.. Tomorrow, I will continue.

Thanks.. I appreciate
• 10-24-2005
7stud
No problem. The stuff I wrote at the bottom was wrong anyway.
• 10-24-2005
7stud
Quote:

if I follow these steps, I will create a linked list by adding a new node to the end of the list right? (I am asking to make sure that I got your point)
Yes.

Quote:

What I want to do is I want to create the first node and add another one to the end of first node than create third one and put it in front of the list..
It's the same process, but you additionally need to store a pointer to the first node in a variable, like root. Then you'll always have a pointer to the first node. To add a new node to the front, you create a node and assign root to its next pointer. After that, all you have to do is redirect root to point to the new node. You can do that in one of two ways:

1) by assigning the address of the new node object to root, e.g.

root = &myNode;

2) or by assigning a pointer to the new node to root, e.g.

root = aPointerToMyNode;