Doubly Linked List (Add F'n & Copy Construct)
I can't figure out what is wrong with my "Addtolist" function and Copy constructor for a doubly linked list. I both a head and tail pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
template<class ItemType>
bool SortedDoublyLinkedList<ItemType>::add(const ItemType &newEntry) {
//TODO - Add an item to the sorted Doubly Linked list
Node<ItemType> *nextNodePtr = new Node<ItemType>();
nextNodePtr->setItem(newEntry);
nextNodePtr->setNext(head);
head = nextNodePtr;
size++;
return true;
}
|
My copy constructor also doesn't seem to want to pass.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
template<class ItemType>
SortedDoublyLinkedList<ItemType>::SortedDoublyLinkedList(const SortedDoublyLinkedList<ItemType> &list) {
//TODO - Implement the copy constructor
size = list.size;
Node<ItemType> *origChainPtr = list.head;
if(origChainPtr == nullptr)
head = nullptr; // original list is empty
else{
// copy first node
head = new Node<ItemType>();
head->setItem(origChainPtr->getItem());
//copy remaining nodes
Node<ItemType> *newChainPtr = head;
origChainPtr = origChainPtr->getNext();
while(origChainPtr != nullptr)
{
// Get next item for original chain
ItemType nextItem = origChainPtr->getItem();
Node<ItemType> *newNodePtr = new Node<ItemType>(nextItem);
newChainPtr->setNext(newNodePtr);
newChainPtr = newChainPtr->getNext();
origChainPtr = origChainPtr->getNext();
}
newChainPtr->setNext(nullptr);
}
}
|
I both a head and tail pointer. |
And yet there is no mention of a tail pointer in all of that code. You might begin there.
I originally had tail pointers but deleted them once I posted here, thought it was wrong. I'll add them back and see if I can solve it.
Topic archived. No new replies allowed.