#include
#include
#include
#define LEN sizeof(struct stu_info)
#define POSTU "\t\t%8s\t%8ld\t%3.1f\t%3.1f\t%3.1f\n"
#define POHEAD "\t\t ÐÕÃû \t ѧºÅ \tÊýѧ\tCÓïÑÔ\tÓ¢Óï\n"
#define PI "%s%ld%f%f%f"
typedef struct stu_info
{
char name[20];
long num;
float math_score;
float C_score;
float Eng_score;
struct stu_info *next;
}STU;
STU *Stu_list_Init(STU *L)//³õʼ»¯
{
L=(STU *)malloc(LEN);
if(L==NULL)
{
exit(0);
}
L->next=NULL;
return L;
}
void Creat_list(STU *L)//´´½¨ ÒÔendΪ½áÊø´´½¨±êÖ¾
{
STU *tail,*p;
tail=L;
while(1)
{
p=(STU *)malloc(LEN);
if(p==NULL)
{
exit(0);
}
printf("\t\tname:");
scanf("%s",p->name);
if(strncmp(p->name,"end",3)==0)
{
free(p);
break;
}
printf("\t\tnumber:");
scanf("%ld",&p->num);
printf("\t\tmath:");
scanf("%f",&p->math_score);
printf("\t\tC:");
scanf("%f",&p->C_score);
printf("\t\tEng:");
scanf("%f",&p->Eng_score);
p->next=NULL;
tail->next=p;
tail=p;
}
tail->next=NULL;
}
void Print_list(STU *L)//´òÓ¡Á´±í
{
STU *p;
p=L->next;
if(p==NULL)
{
return;
}
printf(POHEAD);
while(p!=NULL)
{
printf(POSTU,p->name,p->num,p->math_score,p->C_score,p->Eng_score);
p=p->next;
}
}
void swap(STU *La,STU *Lb)
{
STU tmp,*t;
tmp=*La;
*La=*Lb;
*Lb=tmp;
t=(*La).next;// Do not lost it!Do not lost it! if lost this 3 sentences,that will lost some nodes!
(*La).next=(*Lb).next;
(*Lb).next=t;
}
void Sort_list(STU *L,int len)
{
STU *pa;
int i,j;
// printf("\t\t%d\n",Getlen_list(L)); //Ò²ÓÐÎÊÌâ ÔÚ±¾º¯Êýµ÷ÓÃÇ󳤶Ⱥ¯Êý »áµÃµ½Ò»¸öËæ»úÊý µØÖ·´«µÝ£¿£¿
for(i=0;i
if(pa->num>(pa->next->num))
swap(pa,pa->next);
}
int Getlen_list(STU *L)
{
STU *p;
int n;
p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
p=p->next;
}
return n;
}
int main()
{
STU *stu;
stu=Stu_list_Init(stu);
Creat_list(stu);
Print_list(stu);//ÎÊÌâ³öÔÚÕ⣡£¡
Sort_list(stu,Getlen_list(stu));
Print_list(stu);
return 0;
}
[/code]ÕâôÔËÐÐ ³ÌÐòÊÇÕý³£µÄ £¬ÅÅÐòÊÇÕý³£µÄ
µ«Èç¹û °ÑÖ÷º¯ÊýÒÔÒ»¸ö Print_list(stu);×¢Ê͵ôÖ®ºó ÅÅÐò¾Í³öÏֶδíÎóÁË???
Ï벻ͨ¡£ ¸Ð¾õ Print_list(stu);ºÃÏëûÓиü¸ÄÁ´±íÄÚÈÝ£¬ÎªÊ²Ã´±ØÐë Print_list(stu);Ö®ºóÅÅÐò²ÅÄÜÕý³££¿
JokerHat ÓÚ 2011-12-03 10:25:57·¢±í:
I know ÁË£¬Ð¡´íÎó ÂèµÄ£¬µ÷ÎÒÒ»Ìì¶à£¡£¡