0
我很難弄清楚如何在我的雙鏈表類中實現5的規則。我得到了它們的概念,它只是在如何編寫代碼時丟失了。我曾嘗試過破壞者和複製操作員,但是仍然與其他人一起繼續前進。任何幫助/指導表示讚賞,謝謝。如何在鏈表類中實現賦值運算符
析構函數/副本:
~DList() {
Node* current = front_;
while (current != back_)
{
front_ = front_->next_;
delete current;
current = front_;
}
}
// copy ctor
DList(const DList& rhs) {
const Node* current = rhs.front_;
Node* temp = nullptr;
if (current != back_)
{
front_ = new Node(current->data_);
temp = front_;
current = current->next_;
}
while(current != back_)
{
Node* nn = new Node(current->data_);
temp->next_ = nn;
temp = temp->next_;
current = current->next_;
}
}
DList& operator=(const DList& rhs){ //copy assignment // <<---not sure where to begin
列表構造函數:
DList() {
//Node* front_;
front_ = new Node(); // creating front sentinel
//Node* back_;
back_ = new Node(); // creating back sentinel
//make them point to eachother
front_->next_ = back_;
back_->prev_ = front_;
listSz = 0;
}
主營:
DList<int> list;
DList<int> list2;
DList<int>::const_iterator it;
cout << list.size() << endl;
list.push_front(1);
list.push_front(2);
list.push_back(3);
list.push_front(4);
list.print();
std::cout << endl;
list2.push_front(11);
list2.push_front(12);
list2.push_back(13);
list2.push_front(14);
list2.print();
list2 = list;
list2.print();
我正在尋找深層複製方法 – bb13
@ bb13 - 做了深層複製。我在答案中解釋了它。 – PaulMcKenzie
做了深層複製? – bb13