C++链表不明白,请帮忙教学

来源:互联网 发布:windows应用程序开发 编辑:程序博客网 时间:2024/05/23 02:05

最近,学到C++里面的链表,搞的很混,为什么链表里面要有那么多指针指来指去,比如这样一题目:从键盘上输入若干个学生的数据(数目不定),每个学生数据包括姓名、数学成绩、计算机成绩;输入后计算每个学生两门课程的成绩,最后将所有学生的各科成绩及总成绩输出。在输入姓名时,键入*,表示输入结束。执行结果如下:

                                                   name   math   computer

                                                  1  wangying 80 90

                                                   2  zhangfei  70 60

                                                  3  litao  78 90

                                                   4  *

                                                   wangying,80,90,170

                                                   zhangfei,70,60,130

                                                   litao,78,90,168

书上的程序大致是:

                      struct{定义一些结构成员;

                                   struct student *next;};

struct student *temp,*head,*tail;

temp=new struct student;

head=temp;

tail=head;

cout<<"name math computer"<<endl;

for(i=1;;i++)

{cout<<i<<" ";

cin>>temp->name;

if(temp->name[0]!='*')

{cin>>temp->math>>temp->computer;

temp->sum=temp->math+temp->computer;

temp->next=null;

tail=temp;}

else{

delete temp;

tail->next=null;

break;}

temp->next=new struct student;

temp=temp->next;}

temp=head;

while(temp!=null){

cout......

}

不明白,尤其指针指来指去那一部分,有老师,请帮忙解释一下,谢谢了

 

 

 

原创粉丝点击