我在鏈接列表的深層副本上不斷收到分段錯誤。我在我的「複製構造器」和賦值運算符(運算符=)中使用此深層副本,並得出這樣的結論:這是seg錯誤。返回節點指針的鏈接列表的深層副本
bigint::Node* bigint::deepcopy(bigint::Node* target){
bigint::Node* current = target;
bigint::Node*cpy = new Node;
cpy->digit = current->digit;
Node* const hd = cpy;
current = current->next;
while(current != nullptr){
bigint::Node* tmp = new Node;
tmp->digit = current->digit;
cpy->next = tmp;
cpy = cpy->next;
current = current->next;
}
return hd;
}
我的節點結構是這樣的:
private:
struct Node{
int digit;
Node* next;
};
Node* head;
static Node* deepcopy(Node* target);
我班被關閉,所有的,只是顯示的是私下透露與此有關的功能。提前感謝您的任何建議。
http://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant – macroland
首先,我會建議開始使用unique_ptr和引用,而不是指針,只要你可以。這通常可以減少您現在最小的錯誤。深拷貝究竟意味着什麼以及您打算如何處理該拷貝? –
@MarošBeťko我認爲「深層複製」意味着列表中的所有節點都被複制/克隆。因此,最後你會得到另一個具有相同值的列表 – Thomas