【发布时间】:2016-03-08 19:42:28
【问题描述】:
/********************************************
* Remove the last employee from the list *
********************************************/
void EmployeeList::Remove()
{
newEmployee * nextToEnd = head,
* last = head->Next();
//THIS IS THE PROBLEM
//no nodes
if(head == NULL)
return;
//remove the only employee in the list
if(head->Next()== NULL)
{
cout << "\n\t\tEmployee ID " << head->empID() << " and salary $"
<< head->ySalary() << " have been removed.\n";
head = NULL;
delete head;
}
else
{
// remove the last employee of the list
while(last->Next() != NULL)
{
nextToEnd = last;
last = last->Next();
}
cout << "\n\t\tEmployee ID " << last->empID() << " and salary $"
<< last->ySalary() << " have been removed.\n";
delete last;
nextToEnd->SetNext(NULL);
}
}
尝试从空列表中删除时遇到问题。我知道如果它是空的,我无法删除,但我不会让程序崩溃以显示“员工列表为空。
我指定了我认为问题所在,希望有人能帮我解决。
【问题讨论】:
-
如果
head为NULL,你希望head->Next()做什么?
标签: c++ class linked-list