链表问题 , 首地址的值不能删除,为什么?
#include<iostream>using namespace std;
struct Person
{
char name[20];
int id;
Person* pN;
};
Person* head;
Person* creat()
{
Person* ps;
Person* pEnd;
ps = new Person;
cout<<"please enter the name :"<<endl;
cin>>ps->name;
cout<<"please enter the id :"<<endl;
cin>>ps->id;
pEnd = ps;
head = NULL;
while(ps->id != 0)
{
if(head == NULL)
{
head = ps;
}
else
{
pEnd->pN = ps;
}
pEnd = ps;
ps = new Person;
cout<<"please enter the name :"<<endl;
cin>>ps->name;
cout<<"please enter the id :"<<endl;
cin>>ps->id;
}
pEnd-> pN= NULL;
delete ps;
return (head);
}
void display(Person* head)
{
cout<<"Show this List :"<<endl;
while(head)
{
cout<<"Name is :"<<head->name<<endl;
cout<<"ID is :"<<head->id<<endl;
cout<<"********************************"<<endl;
head = head->pN;
}
}
void del(Person* head,int number)
{
Person* p;
if(!head)
{
cout<<"Empty of head"<<endl;
return ;
}
if(head->id == number)
{
p = head;
head = head->pN;
delete p;
cout<<"The head has delete the number"<<endl;
return ;
}
for(Person* pd = head;pd->pN;pd = pd->pN)
{
if(pd->pN->id == number)
{
p = pd->pN;
pd->pN = p->pN;
delete p;
cout<<"delete the number"<<endl;
return ;
}
}
}
int main()
{
head = creat();
del(head,54);
display(head);
system("pause");
return 0;
}