| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 572 人关注过本帖
标题:链表问题 , 首地址的值不能删除,为什么?
取消只看楼主 加入收藏
巧克力饼干
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-3-27
收藏
 问题点数:0 回复次数:2 
链表问题 , 首地址的值不能删除,为什么?
#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;
}
搜索更多相关主题的帖子: 链表 地址 删除 
2006-04-13 00:48
巧克力饼干
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-3-27
收藏
得分:0 

为啥要用引用指针呢,有点不明白了...

2006-04-13 12:26
巧克力饼干
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-3-27
收藏
得分:0 
解决啦,QQQ
2006-04-13 21:44
快速回复:链表问题 , 首地址的值不能删除,为什么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029292 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved