please fill the blanks Node*origChainPtr / Points to nodes in original chairn Dr
ID: 3745698 • Letter: P
Question
please fill the blanks
Node*origChainPtr / Points to nodes in original chairn Drag and drop the answers below into the dlanK spaces provldel at your left if 3 inai 1st oxpty else /1 Copy first node 4 headPtr->setItem (origChainPtr->getItem O) // Copy remaining nodes Node1temType>* newChainPtr; //Points to last node in new chain newChainPtr headPtr orígChaînPtr orígChainPtr->ge tNext ( ); //Advance original-chain pointer while 5. ( Answers // Get next item from original chain ltenType nex t I temorigChainPtr->getItem(); // Create a new node containing the next item NodesetNext (newNodePtr) / Advance pointer to new last node newChaînPtr newChaînPtr->getNext () ; / Advance original-chain pointer origChainPtrorigChainPtr->getNext // end while newChainPtr->setNext (nullptr)i // Flag end of chain I end if I/ end copy constructorExplanation / Answer
//COPY CONSTRUCTOR
template<class ItemType>
LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList) : itemCount(aList.itemCount)
{
Node<ItemType>* origChainPtr = aList.headPtr; // Points to nodes in original chain
if (origChainPtr == nullptr)
headPtr = nullptr; // Original list is empty
else
{
// Copy first node
headPtr = new Node<ItemType>();
headPtr->setItem(origChainPtr->getItem());
// Copy remaining nodes
Node<ItemType>* newChainPtr = headPtr; // Points to last node in new chain
origChainPtr = origChainPtr->getNext(); // Advance original-chain pointer
while (origChainPtr != nullptr)
{
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
// Link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
// Advance original-chain pointer
origChainPtr = origChainPtr->getNext();
} // end while
newChainPtr->setNext(nullptr); // Flag end of chain
} // end if
} // end copy constructor
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.