我有一個作業問題說:如何編寫遞歸函數的方法定義?
Destructor_Helper是一個遞歸函數,釋放單個鏈表的每個節點。爲destructor_helper編寫一個方法定義。
struct Node
{
string data;
Node *next;
}
void List::~List() {
destructor_helper(head);
}
我的回答是:
void Destructor_Helper(Node *n) {
cout<< n->data << endl;
if (n->next != NULL)
Destructor_Helper(n->next);
}
我的回答是算錯了,會有人幫我找出這個問題。
提示:你在哪裏取消分配? – juanchopanza
它實際上並沒有刪除任何內容。 –
所以加入「delete n;」該功能會改變嗎? – user3467152