输入若干个学生信息(学号 姓名 成绩) 输入学号为0时输入结束 建立一个单向链表 再输入一个成绩值 30
这道题我已在另一地方回答——
http://zhidao.baidu.com/question/255870848.html?sort=6&old=1#answer-1404748948
以下是去掉最后一个条件——“再输入一个成绩值 将成绩大于该值的学生信息输出”。。。的解答!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
int num;
char name[20];
int score;
struct stud_node *next;
};
void main()
{
struct stud_node *head,*tail, *p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);
head=tail=NULL;
scanf("%d", &num);
while(num != 0){
scanf("%s%d",name,&score);
p=(struct stud_node*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
for(p=head; p!=NULL; p=p->next)
printf("%d %s %d\n", p->num,p->name,p->score);
}
#include<stdlib.h>
#include<string.h>
struct stud_node{
int num;
char name[20];
int score;
struct stud_node*next;
};
int main(void)
{
struct stud_node *head,*p,*tail;
int num,score,m;
char name[20];
struct stud_node *ptr;
int size=sizeof(struct stud_node);
head=tail=NULL;
printf("Input num,nameandscore:\n");
scanf("%d",&num);
while(num!=0){
scanf("%s%d",name,&score);
p=(struct stud_node*)malloc(size);
p->num=num; strcpy(p->name,name);
p->score=score;
if(head==NULL){
head=p;
tail = p;
}
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
printf("please enter m:");
scanf("%d",&m);
if(head==NULL){
printf("\n no records");
return 0;
}
for(ptr=head;ptr;ptr=ptr->next) {
if(ptr->score>=m)
printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score);
}
return 0;
}
2012-04-15