题目描述
初步掌握了链表的建立,下面我们再来一个简单的链表的操作。
输入一些学生的信息,包括学号、姓名、成绩。
现在要求你用函数创建一个学生库的链表。
struct node
{
....
};
struct node * Create_Stu_Doc();
完成函数内链表的创建工作。
输入
输入一些学生的信息,每个学生信息一行,分别为学号、姓名和成绩,中间用空格隔开,其中学号和成绩均为整数,姓名为不超过15个仅包含大小写字母的字符。
如果输入的一行是非正整数时,表示结束
输出
按照输入的顺序建立链表,并输出所有高于平均分同学的学号和分数,每行2个整数,中间用空格隔开。
样例输入 Copy
1001 xiangwang 90 1002 xiaoli 85 1003 xiaohong 97 1004 xiaoma 76 -1
样例输出 Copy
1001 90 1003 97
提示
#include<iostream> #include<malloc.h> #include<cstring> using namespace std; struct node { //请完成结构体内部 }; struct node * Create_Stu_Doc(); int main() { struct node *head,*p; float s=0; int n=0; head=Create_Stu_Doc(); for(p=head;p!=NULL;p=p->next) { s+=p->score; n++; } s=s/n; for(p=head;p!=NULL;p=p->next) { if((p->score)>s) { printf("%d %d\n",p->num,p->score); } } return 0; } //完成Create_Stu_Doc()函数的设计 ...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int number;
char name[20];
int score;
struct node *next;
};
struct node *Create_Stu_Doc();
int main()
{
struct node *head,*p;
int n=0;
float s=0;
head=NULL;
head=Create_Stu_Doc();
for(p=head;p!=NULL;p=p->next)
{
s+=p->score;
n++;
}
s=s/n;
for(p=head;p!=NULL;p=p->next)
{
if((p->score)>s)
{
printf("%d %d\n",p->number,p->score);
}
}
return 0;
}
struct node *Create_Stu_Doc()
{
struct node *head,*tail,*p;
int n,s;
char a[20];
head=tail=NULL;
scanf("%d",&n);
while(n>0)
{
scanf("%s%d",a,&s);
p=(struct node *)malloc(sizeof(struct node));
p->number=n;
strcpy(p->name,a);
p->score=s;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
tail->next=p;
}
tail=p;
scanf("%d",&n);
}
return head;
}