problem inserting node in middle of list

This is a discussion on problem inserting node in middle of list within the C++ Programming forums, part of the General Programming Boards category; I'm making a singly linked list. It has a problem inserting nodes in the middle. Code: void LinkedList::createNode(string aName) { ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    117

    problem inserting node in middle of list

    I'm making a singly linked list. It has a problem inserting nodes in the middle.

    Code:
    void LinkedList::createNode(string aName)
    {
        if(startPointer == NULL)
        {
            startPointer = new Node;//Node is a struct
            (*startPointer).next = NULL;
            (*startPointer).name = aName;
        }
        else
        {
            Node *temp = startPointer;
            while((*temp).next != NULL)
                temp = (*temp).next;
            (*temp).next = new Node;
            temp = (*temp).next;
            (*temp).name = aName;
        }
    }
    
    void LinkedList::createNode(string aName, unsigned int position)
    {
        Node *inserting = new Node(aName);
        if(position == 0)
        {
            (*inserting).next = startPointer;//NOT startPointer->next
            startPointer = inserting;
        }
        else{
            Node *temp, *previous;
            temp = startPointer;
            for(unsigned int i = 1; i < position; i++)
            {
                previous = temp;
                if((*temp).next != NULL)
                    temp = (*temp).next;
                else
                {
                    std::cout << "The end of the list has been reached so the node will be added to the end." << std::endl;
                    createNode(aName);
                    return;//causes method to end prematurally, which we want
                }
            }/*This doesn't work*/
            Node *inserting = new Node;
    	(*inserting).name = aName;
            (*inserting).next = temp;
            std::cout << "done setting where the new node points to" << std::endl;
            std::cout << "previous->next: " << (*previous).next << std::endl;//crashes here
            (*previous).next = inserting;
            std::cout << "done changing where the node before the new node points to (which is the new node)" << std::endl;
        }
    }
    Inserting at the begining and inserting at the end (if done by the void LinkedList::createNode(string aName) function) works fine.
    Last edited by c_weed; 12-22-2010 at 01:37 PM. Reason: Should have been createNode(aName); not createNode(name);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    else
                {
                    std::cout << "The end of the list has been reached so the node will be added to the end." << std::endl;
                    createNode(name);
                    return;//causes method to end prematurally, which we want
                }
    So when do you tie that new node to the end of the list?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    117
    Quote Originally Posted by tabstop View Post
    Code:
    else
                {
                    std::cout << "The end of the list has been reached so the node will be added to the end." << std::endl;
                    createNode(name);
                    return;//causes method to end prematurally, which we want
                }
    So when do you tie that new node to the end of the list?

    createNode(name); does it.

    Sorry I ment createNode(aName)

  4. #4
    zfk
    zfk is offline
    Registered User
    Join Date
    Dec 2010
    Location
    China
    Posts
    7
    Code:
    void LinkedList::createNode(string aName, unsigned int position)
    {
    	Node *inserting = new Node(aName);
    	if(position == 0)
    	{
    		(*inserting).next = startPointer;//NOT startPointer->next
    		startPointer = inserting;
    	}
    	else{
    		Node *temp, *previous;
    		temp = startPointer;
    		for(unsigned int i = 1; i < position; i++)  //if position = 1. not step into for loop.point of previous is null.
    		{
    			previous = temp;
    			if((*temp).next != NULL)
    				temp = (*temp).next;
    			else
    			{
    				std::cout << "The end of the list has been reached so the node will be added to the end." << std::endl;
    				createNode(aName);
    				return;//causes method to end prematurally, which we want
    			}
    		}/*This doesn't work*/
    		Node *inserting = new Node; //the inserting is definit. can cause memery lead.
    		(*inserting).name = aName;
    		(*inserting).next = temp;
    		std::cout << "done setting where the new node points to" << std::endl;
    		std::cout << "previous->next: " << (*previous).next << std::endl;//crashes here
    		(*previous).next = inserting;
    		std::cout << "done changing where the node before the new node points to (which is the new node)" << std::endl;
    	}
    }

    look my note
    Last edited by zfk; 12-22-2010 at 08:15 PM. Reason: change

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21