Thread: how to add a note to the begining of linked list

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    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
    Last edited by merixa; 10-24-2005 at 01:31 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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
    Last edited by 7stud; 10-24-2005 at 01:35 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    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. 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
    Last edited by merixa; 10-24-2005 at 01:45 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    thanks for replying but I tried to draw diagram. I got the idea but I can't put it on code.
    To create a linked list, you can follow these steps:

    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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    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..

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;

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

    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;

    3. set root to point to the new node
    root = conductor;
    Last edited by 7stud; 10-24-2005 at 02:07 AM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    @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

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    No problem. The stuff I wrote at the bottom was wrong anyway.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

    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;
    Last edited by 7stud; 10-24-2005 at 03:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  2. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM