奇怪的全局结构指针,郁闷了一下午了,请教各位!

txhby0395 2005-05-13 09:12:12
调试平台WIN XP VC++6.0
#include <iostream.h>

struct Student
{
long number;
float score;
Student *next;
};

Student* head;

Student* Create()
{
Student* pS;
Student* pEnd;
pS=new Student;
cin>>pS->number>>pS->score;
head=NULL;
pEnd=pS;

while (pS->number!=0)
{
if (head==NULL)
head=pS;
else
pEnd->next=pS;

pEnd=pS;
pS=new Student;
cin>>pS->number>>pS->score;
}

pEnd->next=NULL;
delete pS;
return (head);
}

void ShowList(Student* head)
{

while (head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

void Insert(Student* head,Student* stud)
{
if (head==NULL)
{
head=stud;
stud->next=NULL;
return ;
}

else if (head->number>stud->number)
{
stud->next=head;
head=stud;
return ;
}

else
{
Student* pGuard=head;
while ((pGuard->next)&&((pGuard->next->number)>(stud->number)))
{
stud->next=pGuard->next;
pGuard->next=stud;
}
return ;
}

}
void main()
{
Student ps;
ps.number=36;
ps.score=3.8;
head=Create();
Insert(head,&ps);//全局head的地址无任何改变
ShowList(head);
}

////////按照以下的写法就OK,不知道为什么///////////////
#include <iostream.h>

struct Student
{
long number;
float score;
Student *next;
};

Student* head;

Student* Create()
{
Student* pS;
Student* pEnd;
pS=new Student;
cin>>pS->number>>pS->score;
head=NULL;
pEnd=pS;

while (pS->number!=0)
{
if (head==NULL)
head=pS;
else
pEnd->next=pS;

pEnd=pS;
pS=new Student;
cin>>pS->number>>pS->score;
}

pEnd->next=NULL;
delete pS;
return (head);
}

void ShowList(Student* head)
{
while (head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

Student* Insert(Student* head,Student* stud)
{
if (head==NULL)
{
head=stud;
stud->next=NULL;
return head;
}

else if (head->number>stud->number)
{
stud->next=head;
head=stud;
return head;
}

else
{
Student* pGuard=head;
while ((pGuard->next)&&((pGuard->next->number)>(stud->number)))
{
stud->next=pGuard->next;
pGuard->next=stud;
}
return head;
}

}
void main()
{
Student ps;
ps.number=36;
ps.score=3.8;
head=Create();
head=Insert(head,&ps);
ShowList(head);
}
...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
llf_hust 2005-05-14
  • 打赏
  • 举报
回复
Insert(head,&ps);//全局head的地址无任何改变
//要用指针的指针才能够改变
zhouhuahai 2005-05-14
  • 打赏
  • 举报
回复
另一种作法是: 用*&即可. 比如
void Insert(Student* head,Student* stud)
改为void Insert(Student*& head,Student*& stud)
ParkPeng 2005-05-14
  • 打赏
  • 举报
回复
用C或者C++都是一样的道理。
Insert(head,&ps);只能改变head指向的单元的内容;对head操作,只是操作一个局部指针变量,你改变它,当然不会影响到实参了。链表可要注意了:head指向的单元包含指针(next),你可以通过head改变该链表,如在链表中部或者尾部添加或者删除节点;但操作头部会失败。
用函数操作链表有2种途径:
1,形参用二重指针,实参传入head的地址(如llf_hust() 所述)。你可以随心所欲地操纵该链表了。
2,传入head,函数再返回一个头指针。将再函数中改变的形参返回,一样的效果。
lonelyforest 2005-05-14
  • 打赏
  • 举报
回复
你为什么不设计一个student的类呢? 这样不是更加的方便吗? 用了C++ 而不用其中的类, 面向对象,还不如用C 呢!
safe_man 2005-05-14
  • 打赏
  • 举报
回复
全局变量名不要和局部变量一样,让人看了不清楚。建议在全局变量前加g_
jingyueid 2005-05-14
  • 打赏
  • 举报
回复
head=Insert(head,&ps);
这样的写法会将操作后的head地址返回给head。

而你前一种写法里面
Insert(head,&ps);
head是以一个指针的身份进去的,如果要修改head的指向,你需要对指针进行取址操作,使用指针的引用或指向指针的指针来完成效果。
Salam2001 2005-05-13
  • 打赏
  • 举报
回复
回复人:zhousqy(C++匪徒) ( 五级(中级)) 信誉:100 2005-05-13 21:22:00 得分:0
?
加点分撒。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

哈哈 不厚道...
zhousqy 2005-05-13
  • 打赏
  • 举报
回复
加点分撒。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧