我在理解爲什麼當我創建兩個或多個節點時(如下所示),函數void del_end()
將只刪除char name[20]
而不是整個節點。我如何解決這個問題沒有內存泄漏?刪除雙向鏈表中的節點(C++)
#include <iostream>
using namespace std;
struct node
{
char name[20];
char profession[20];
int age;
node *nxt;
node *prv;
};
node *start_ptr = NULL;
void del_end()
{
node *temp, *temp2;
temp = start_ptr;
if (start_ptr == NULL)
cout << "Can't delete: there are no nodes" << endl;
else if (start_ptr != NULL && start_ptr->nxt == NULL)
{start_ptr=NULL;}
else
{
while (temp->nxt != NULL)
{
temp = temp->nxt;
}
temp2=temp->prv;
delete temp;
temp->nxt= NULL;
}
}
這功課嗎? – Karmastan 2010-07-06 18:46:28
我認爲這是作業嗎?如果是這種情況,請添加作業標籤。 – pmr 2010-07-06 18:47:24