【发布时间】:2011-03-12 11:28:58
【问题描述】:
我无法理解为什么当我创建两个或更多节点(如下所示)时,函数void del_end()只会删除char name[20]而不是整个节点。如何在不发生内存泄漏的情况下解决此问题?
#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;
}
}
【问题讨论】:
-
我认为这是家庭作业?如果是这种情况,请添加作业标签。
标签: c++ linked-list destructor