使用链接列表时,我遇到了很多麻烦实现深层复制的问题。我很确定问题是使用otherList.listData->给我一个指向原始列表中的数据的指针,而不是复制这些值。但是,我对如何直接访问数据感到困惑。我以为你可以放弃这些指针,但我必须有错误的语法。对于CourseList类所需的数据,也没有get/set方法。使用链接列表复制Ctor
有没有人有任何想法?
头文件
class CourseList
{
private:
struct CourseNode
{
int CRN;
char letterGrade;
CourseNode *next;
};
int length;
CourseNode *listData;
public:
CourseList();
CourseList(const CourseList& otherList);
~CourseList();
};
CPP文件
CourseList::CourseList(const CourseList& otherList)
{
length = otherList.length;
for (int i = 0; i < length; i++)
{
CourseNode* temp = new CourseNode;
temp->CRN = otherList.listData->CRN;
temp->letterGrade = otherList.listData->letterGrade;
temp->next = otherList.listData->next;
listData = temp;
}
}
敢肯定你正在试图做的[这样的事情(http://ideone.com/ JcFPfR)。 – WhozCraig 2014-10-22 11:49:44