template void Sequence0::copyNodes(NodeRecord*& toP, NodeRecord* fromP) // produ
ID: 3681585 • Letter: T
Question
template void Sequence0::copyNodes(NodeRecord*& toP, NodeRecord* fromP)
// produces: toP
// requires: Alive(fromP) || Null(fromP)
// ensures: a deep copy of the linked list addressed by fromP is created
// the address of the 1st node in the new linked list is produced through toP
// fromP's linked list is left unchanged
// Use the assignment operator to copy the value found in the node's value field
// Must use iteration, not recursion
{
//TODO: duplicateLinkedList using iteration
if (fromP == NULL) { toP = NULL; }
else {
NodeRecord* pnt1;
NodeRecord* pnt2;
pnt1 = new NodeRecord;
pnt1->value = fromP->value;
toP = pnt1;
pnt2 = toP;
while (fromP->next != NULL){
NodeRecord* newNode;
newNode = new NodeRecord;
fromP = fromP->next;
newNode->value = fromP->value;
newNode->next = pnt2;
pnt2 = pnt2->next; } }
} // duplicateLinkedList
Hi guys, I have an error on my copyNode function in C++. I have done other setting in my compiler. only i need this function be fix. I do not need other suggustion like recursion or return type. It should be like its contract which i am going to implement. Again please do not use other theory or method to fix. If any of you know to fix reply because I posted my questions but the guys did not answer correctly and they copeid from online resouces.
Explanation / Answer
template void Sequence0::copyNodes(NodeRecord*& toP, NodeRecord* fromP)
// produces: toP
// requires: Alive(fromP) || Null(fromP)
// ensures: a deep copy of the linked list addressed by fromP is created
// the address of the 1st node in the new linked list is produced through toP
// fromP's linked list is left unchanged
// Use the assignment operator to copy the value found in the node's value field
// Must use iteration, not recursion
{
//TODO: duplicateLinkedList using iteration
if (fromP == NULL) { toP = NULL; }
else {
NodeRecord new1 = new NodeRecord;
new1->value = fromP->value
toP = new1;
fromP = fromP->Next;
while (fromP->next != NULL){
NodeRecord* newNode = new NodeRecord;
newNode->value = fromP->value;
new1->next = newNode;
fromP = fromP->next;
new1 = new1->next;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.