Trying to get a linked list copy function working, my code is compiling hower it
ID: 3796285 • Letter: T
Question
Trying to get a linked list copy function working, my code is compiling hower it is not copying when I test it. Can you review my code and suggest some changes or suggest an enitrely new code?
template <class T>
void ListType::copy(const ListType& otherList) {
NodeType *newNode;
NodeType *current;
if (first != nullptr)
destroy();
if (otherList.first == nullptr)
{
first = nullptr;
last = nullptr;
count = 0;
}
else {
current = otherList.first;
count = otherList.count;
first = new NodeType;
first->info = current->info;
first->link = nullptr;
last = first;
current = current->link;
while (current != nullptr) {
newNode = new NodeType;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
MAIN WHERE I AM TESTING IT:
#include
#include
#include "OListType.h"
#include "UListType.h"
using namespace std;
template
void testCopyConstructor(UListType);
int main()
{
UListType list2;
for (int num = 0; num < 20; ++num)
list2.insert(rand() % 100);
cout << "List2: " << list2 << endl;
cout << endl;
testCopyConstructor(list2);
cout << endl;
UListType ulist2(list2);
cout << ulist2 << endl;
cout << endl;
cout << "Testing Copy Constructor Complete if all three lists match." << endl;
cout << endl;
system("pause");
return 0;
}
template
void testCopyConstructor(UListType otherList) {
cout << otherList << endl;
}
Explanation / Answer
Hi Friend, Your 'copy' method is correct. But testing method is not correct .
You are not calling copy method anywhere.
I have modified main method and you can please let me know in case of any issue.
// THis is correct function
template <class T>
void ListType<T>::copy(const ListType& otherList) {
NodeType *newNode;
NodeType *current;
if (first != nullptr)
destroy();
if (otherList.first == nullptr)
{
first = nullptr;
last = nullptr;
count = 0;
}
else {
current = otherList.first;
count = otherList.count;
first = new NodeType;
first->info = current->info;
first->link = nullptr;
last = first;
current = current->link;
while (current != nullptr) {
newNode = new NodeType;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
MAIN WHERE I AM TESTING IT:
#include <iostream>
#include <cstdlib>
#include "OListType.h"
#include "UListType.h"
using namespace std;
template
void testCopyConstructor(UListType);
int main()
{
UListType list2;
for (int num = 0; num < 20; ++num)
list2.insert(rand() % 100);
cout << "List2: " << list2 << endl;
cout << endl;
// THis method is not testing any copy construction.
// do not calling copy method anywhere
// testCopyConstructor(list2); // this is not class method
UListType copyList; // creating a new empty list
copyList.copy(list2); // copying the content if list2 in copyList
cout << copyList << endl; // printing the content of copyList
cout << endl;
UListType ulist2(list2);
cout << ulist2 << endl;
cout << endl;
cout << "Testing Copy Constructor Complete if all three lists match." << endl;
cout << endl;
system("pause");
return 0;
}
// here , how you are testing the copy construction
template
void testCopyConstructor(UListType otherList) {
cout << otherList << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.