注册 登录
编程论坛 C++教室

代码有点看不懂

jjg 发布于 2009-10-18 16:13, 823 次点击
#include <iostream>
using namespace std;


struct student
{
    long number;
    float score;
    student * next;
};
student * head;
student * create()
{
    student * ps;
    student * pend;
    ps=new student;
    cin>>ps->number;cin>>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)
{
    cout<<"now the items of list are";
    while(head)
    {
        cout<<head->number<<","<<head->score<<endl;
        head=head->next;
    }
}
void main()
{
    showlist(create());
}
上面红色代码是一样的,有什么不同的作用吗?
3 回复
#2
blueboy820062009-10-18 22:34
很常用的方法嘛~
第一个是要求输入,这样好作 while(ps->number!=0)的判断;
第二个则是while中的,要求循环输入,直到ps->number==0也就不满足while条件,从而退出.
#3
caolihui10082009-10-19 09:20
就是建立一个链表嘛,哪里看不懂呢?
#4
zodiac2072009-10-19 15:32
我想拿张约画画应该比较明了吧,如果看不明白的话
1