找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 985|回复: 5

链表问题 , 首地址的值不能删除,为什么?

[复制链接]
发表于 2009-11-3 00:53:27 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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;
}
发表于 2009-11-3 00:53:29 | 显示全部楼层 IP:江苏扬州
先抱怨一下,这个问题我好像以前解决过。不会是哪本书上的习题吧?
回复

使用道具 举报

发表于 2009-11-3 00:53:30 | 显示全部楼层 IP:江苏扬州
void del(Person* head,int number)

修正为void del(Person*& head,int number)
回复

使用道具 举报

发表于 2009-11-3 00:53:32 | 显示全部楼层 IP:江苏扬州
为啥要用引用指针呢,有点不明白了...
回复

使用道具 举报

发表于 2009-11-3 00:53:34 | 显示全部楼层 IP:江苏扬州
通过打印函数内和函数外的head地址,你会发现函数内的head与函数外的head不一样。有点象值传递和引用传递。


这里你也可以利用head的全局变量的特点,把del和display的head参数去掉。运行也能成功。
回复

使用道具 举报

发表于 2009-11-3 00:53:35 | 显示全部楼层 IP:江苏扬州
解决啦,QQQ
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 05:26 , Processed in 0.155665 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表