#include<iostream> #include<string> using namespace std; class Student { public: static int number; Student *build(int); friend void display(Student *head); Student *next; string name; float score; private: }; //数据链表 Student *build(int length) { Student *hold,*temp,*head; int counter=1; head=new Student; hold=head; hold->next=NULL; do{ cout<<"输入学生的姓名:"; cin>>hold->name; cout<<"输入学生的成绩:"; cin>>hold->score; counter++; temp=new Student; hold->next=temp; hold=temp; }while(counter<=length); hold->next=NULL; return head; } //打印数据 void display(Student*head) { Student *hold; hold=head; do{ cout<<"姓名"<<'\t'<<"成绩"<<endl; cout<<hold->name<<'\t'<<hold->score<<endl; hold=hold->next; }while(hold!=NULL); } int main() { Student *head; int length=3; head=build(length); display(head); return 0; }
输出里有个BUG 多输出了一个变量;