链表实现,并按年龄排序。
# include <stdio.h>
# include <stdlib.h>
typedef struct Node
{
char num[20];
char name[20];
int age;
char sex[3];
struct Node *next;
};
struct Node *Create_List(int n)
{
int j;
struct Node *rear,*s,*CL;
CL=(struct Node*)malloc(sizeof(struct Node));
rear=CL;
for(j=0;j<n;j++)
{
s=(struct Node*)malloc(sizeof(struct Node));
scanf("%s%s%d%s",s->num,s->name,&s->age,s->sex);
rear->next=s;
rear=s;
}
rear->next=NULL;
return CL;
}
struct Node* fun(struct Node*CL)
{
struct Node*p,*q,*s;
p=CL;
q=CL;
while(p->next!=NULL)
{
p=p->next;
while(q->next!=NULL)
{
q=q->next;
if(p->age>q->age)
{
s=p;
p=q;
q=s;
}
}
}
return CL;
}
void print(struct Node*CL)
{
struct Node*p;
p=CL;
while(p->next!=NULL)
{
p=p->next;
printf("%s\t%s\t%d\t%s\n",p->num,p->name,p->age,p->sex);
}
}
void main()
{
int n;
struct Node*CL,*p;
printf("请输入人数n:");
scanf("%d",&n);
CL=Create_List(n);
printf("学号\t\t姓名\t年龄\t性别\t\n");
p=fun(CL);
print(p);
}