#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct member
{
char name[25];
char club[25];
float height;
int age;
struct member *next;
};
typedef struct member tmber;
tmber *creat()
{
tmber *head,*p1,*p2;
p1=p2=(tmber*)malloc(sizeof(tmber));
printf("Please enter player information:\n");
scanf("%s%s%f%d",p1->name,p1->club,&p1->height,&p1->age);
head=NULL;
while(p1->age!=0)
{
if(head==NULL) head=p1;
else p2->next=p1;
p2=p1;
p1=(tmber*)malloc(sizeof(tmber));
scanf("%s%s%f%d",p1->name,p1->club,&p1->height,&p1->age);
}
p2->next=NULL;
return head;
}
tmber swap(tmber *p2,tmber *p) //交换每个节点的数据
{
int a;
float b;
char s1[25],s2[25];
a=p2->age; p2->age=p->age; p->age=a;
b=p2->height; p2->height=p->height; p->height=b;
strcpy(s1,p2->name); strcpy(p2->name,p->name); strcpy(p->club,s2);
strcpy(s2,p2->club); strcpy(p2->club,p->club); strcpy(p->club,s2);
}
void selectsort(tmber *head,int n)
{
tmber *p1,*p2,*p;
int a;
float b;
char s1[25],s2[25];
p=p2=head;
p1=head->next;
while(p->next!=NULL)
{
do{
switch(n){
case 1:if(p1->height>p2->height)//按身高降序排列
p2=p1;break;
case 2:if(p1->age>p2->age) //按年龄降序排列
p2=p1;break;
default:printf("input error!\n");
}
p1=p1->next;
}while(p1!=NULL);
swap(p2,p);
p2=p=p->next;
p1=p->next;
}
}
void list(tmber *p)
{
do{
printf("%s,%s,%.1f,%d\n",p->name,p->club,p->height,p->age);
p=p->next;
}while(p!=NULL);
}
int main()
{
int n=1;
tmber *head;
head=creat();
selectsort(head,n);
printf("按身高降序排列输出:\n");
list(head);
n=n+1;
selectsort(head,n);
printf("按年龄降序排列输出:\n");
list(head);
return 0;
}
为足球队员建立记录,要有姓名、所属俱乐部、身高、年龄、按身高降序排列输出,再按年龄降序排列输出
最新推荐文章于 2022-09-20 21:54:12 发布