使用鏈接列表時,我遇到了很多麻煩實現深層複製的問題。我很確定問題是使用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