小编真是无语,因为一个输入创建列表总是创不起来...
#include <iostream>
using namespace std;
struct student
{
int num;
int score;
student*next;
};
int n;//用来计数
student*creat()
{
student*head,*p1,*p2;
head=NULL;//头节点赋值
n=0;
p1=p2=new student;
cout<<"请输入学生的学号与成绩,退出学号请输入0";
cin>>p1->num>>p1->score;//输入信息
while(p1->num!=0){//判断输入的信息是否有效
n=n+1;//计数的变化
if(n==1) {
head=p1;//头节点赋值
}
else {
p2->next=p1;//连接起整个链表点
}
p2=p1;//储存
p1=new student;//开辟新空间
cin>>p1->num>>p1->score;//信息输入
}
p2->next=NULL;//无效退出
return (head);
}
void print(student*head)
{
student*p;
p=head;
while(p!=NULL)
{
cout<<p->num<<" "<<p->score;
p=p->next;
}
}
int main()
{
student *head;
head=creat( );
print(head);
return 0;
}