如果列表中有3个或更多元素,我可以成功删除最后一个元素。但是当列表中有两个元素时,每次我尝试删除最后一个元素时,它也会删除第一个元素。
node *tail = new node;
admintemp = adminhead;
while (admintemp->next!=NULL)
{
tail=admintemp;
admintemp=admintemp->next;
}
if (tail)
{
tail->next=NULL;
}
delete admintemp;
答案 0 :(得分:0)
Explaination:
With the help of temp we reach upto last node so as far as your problem is
concerned imagine you have two nodes only.so with temp we reach to second
node
Now with the help of temp1 we traverse up to temp,we make temp1's next to
NULL coz now it is end.free memory of temp with help of free(temp)
void Delete()
{
class Node* temp,*temp1;
temp=temp1=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
while(temp1->next!=temp)
{
temp1=temp1->next;
}
temp1->next=NULL;
free(temp);
}