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:
Here is the add function that is also part of the linkList class: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; } } }
Any ideas why this is happening?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; } }