Thread: traversing a linked list with a node and list class

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    traversing a linked list with a node and list class

    I am having trouble finding a way to traverse the linked list. I am trying to write the copy constructor, but keep having a memory crash. The nodes seem to be being created but the data is garbage.

    Here is the constructor:

    Code:
    linkList::linkList(const linkList &originalList)
    {
    	linkList tmpList;
    	if (_debugging)
    		cout << "tmpList created." << endl;
    
    	for (node *i = originalList._firstElement; i->next != NULL; i = i->next)
    	{
    		tmpList.add(i);
    		
    		if (i->next == NULL)
    		{
    			break;
    		}
    	}
    
    }
    Here is the add function that is also part of the linkList class:

    Code:
    void	linkList::add(node *newnode)
    {
    
    	node * newNode = newnode;
    	if (isEmpty())
    	{
    		_firstElement = newNode = _lastElement;
    		_numElements ++;
    
    		if (_debugging)
    			cout << "New node added to the list." << endl;
    	}
    	else
    	{
    		_lastElement->next = newNode;
    		_lastElement = newNode;
    		_numElements ++;
    
    		if (_debugging)
    			cout << "New node added to the list." << endl;
    	}
    
    }
    Any ideas why this is happening?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    I have made the changes you suggested, but I am getting the same results.

    Maybe I am implementing this in my test file incorrectly. I am trying to create a list using another list. Like this:

    Code:
    linkList list2(list1);  // Where list1 is an already created list with 3 node objects
    Is this what the copy constructor is for? Or am I confusing the copy constructor with the assignment operator?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    linkList list2(list1);

    the above creates list2 by copying data values in list1 into list2 whereas below is creating tmpList, not list2. tmpList is a local variable to the constructor, not the list you are trying to create.

    Code:
    linkList::linkList(const linkList &originalList)
    {
       linkList tmpList;
    
       for (node *i = originalList._firstElement; i->next != NULL; i = i->next)
      {
         tmpList.add(i);
         //etc.
    [/
    Try this instead:
    Code:
    linkList::linkList(const linkList &originalList)
    {
       for (node *i = originalList._firstElement; i->next != NULL; i = i->next)
      {
         add(i);
         //or 
         //this->add(i); if you want to be explicit.

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, 04:09 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM