新人,求指导或教程!
求写一函数对链表进行排序,最好能注上详细解释,书上没相关的,自己想了很多算法,但都写不下来,最好是用插入法的。
程序代码:#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
typedef struct student
{
int mum;
char name[20];
int age;
char sex[5];//w and M
// int birthday[2];
// char address[50];
// int photo;
// char Email[20];
struct student *next;
}st;
int n;
//创建+输入
st *creat(void)
{
void scan(st *q);
st *head;
st *p1,*p2;
n=0;
p1=p2=(st *)malloc(sizeof(st));
scan(p1);
head=NULL;
while(p1->mum!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(st *)malloc(sizeof(st));
scan(p1);
}
p2->next=NULL;
return(head);
}
void scan(st *q)
{
printf("请输入学号\n");
scanf("%d",&(*q).mum);
fflush(stdin);
if(q->mum!=0)
{
printf("请输入姓名\n");
scanf("%s",(*q).name);
fflush(stdin);
printf("请输入年龄\n");
scanf("%d",&(*q).age);
fflush(stdin);
printf("请输入性别\n");
scanf("%s",(*q).sex);
fflush(stdin);
/* printf("请输入出生年月(年-月-日)");
scanf("%d-%d-%d",(*q).birthday[0],(*q).birthday[1],(*q).birthday[2]);
printf("请输入家庭住址");
scanf("%f",(*q).address);
printf("请输入电话号码");
scanf("%d",(*q).photo);
printf("请输入E-mail");
scanf("%f",(*q).Email); */
}
}
void print(st *m)
{
printf( "%4d %4s %2d %2s\n",(*m).mum,(*m).name,(*m).age,(*m).sex);
}
void print1(st *head)
{
st *k;
k=head;
printf("mum name age sex\n");
if(head!=NULL)
do
{
print(k);
k=k->next;
}while(k!=NULL);
}
int main()
{
st *head;
int m;
head=creat();
print1(head);
// charu(head);
// print1(head);
// del(head);
// print1(head);
check_mum(head);
}
最好是能提供以此为基础,以MUM从小到大来排序的






