#include <iostream>
#include <stdlib.h>
using namespace std;
struct stu
{
int num;
char name[20];
float score;
struct stu *next;
};
struct stu *creat(int n)
{
struct stu *head,*pf,*pb;
cout<<"输入结点数:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
pb=(struct stu*)malloc(sizeof(struct stu));
cout<<"请输入学生学号,姓名,成绩:"<<endl;
cin>>pb->num>>pb->name>>pb->score;
if(i==0)pf=head=pb;
else pf->next=pb;pb->next=NULL;
pf=pb;
}
return (head);
}
struct stu *insert(struct stu *head,struct stu *pi)
{
struct stu *pb,*pf;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head=pb)head=pi;
else pf->next=pi;
pi->next=pb;
}
else
{
pb->next=pi;
pi->next=NULL;
}
}
return (head);
}
void display(struct stu* head)
{
cout<<"学生相关信息和成绩如下:"<<endl;
cout<<"学号\t姓名\t成绩:"<<endl;
while(head!=NULL)
{
cout<<head->num<<"\t"<<head->name<<"\t"<<head->score<<endl;
head=head->next;
}
}
int main()
{
struct stu *head,*p;
int k,num; //警告在这行和下一行
head=creat(k); //警告在这行和上一行
cin>>k;
display(head);
p=(struct stu *)malloc(sizeof(struct stu));
cin>>p->num>>p->name>>p->score;
head=insert(head,p);
display(head);
return 0;
}
各位帮小弟看看,在此先谢过!
[此贴子已经被作者于2006-11-23 15:23:57编辑过]