指针问题求解
程序代码:score *rank(score *head)
{
score *p,*max;
int i,j,x;
float chengji;
char t[10];
if(head == NULL)
{
printf("没有任何学生资料\n");
return(head);
}
max = p = head;
printf("********************************************************************************\n");
printf("1按学生学号排序\t2按学生姓名排序3按语文成绩排序\n");
printf("4按英语成绩排序\t5按数学成绩排序\t\n");
printf("********************************************************************************\n");
printf("请选择排序类型:");
scanf("%d",&x);
switch(x)
{
case 1:
for(i = 1;i < k; i++)
{
for(j = i+1; j < k; j++)
{
max = p;
p = p -> next;
if(max -> number > p -> number)
{
n = max -> number;
max -> number = p -> number;
p -> number = n;
strcpy(t,max -> name);
strcpy(max -> name,p -> name);
strcpy(p -> name,t);
chengji = max -> yuwen;
max -> yuwen = p -> yuwen;
p -> yuwen = chengji;
chengji = max -> shuxue;
max -> shuxue = p -> shuxue;
p -> shuxue = chengji;
chengji = max -> yingyu;
max -> yingyu = p -> yingyu;
p -> yingyu = chengji;
}
}
max = head;
p = head;
}
show(head);
break;
case 2:
for(i = 1;i < k; i++)
{
for(j = i+1; j < k; j++)
{
max = p;
p = p -> next;
if(strcmp(max -> name, p -> name) > 0)
{
n = max -> number;
max -> number = p -> number;
p -> number = n;
strcpy(t,max -> name);
strcpy(max -> name,p -> name);
strcpy(p -> name,t);
chengji = max -> yuwen;
max -> yuwen = p -> yuwen;
p -> yuwen = chengji;
chengji = max -> shuxue;
max -> shuxue = p -> shuxue;
p -> shuxue = chengji;
chengji = max -> yingyu;
max -> yingyu = p -> yingyu;
p -> yingyu = chengji;
}
}
max = head;
p = head;
}
show(head);
break;
case 3:
for(i = 1;i < k; i++)
{
for(j = i+1; j < k; j++)
{
max = p;
p = p -> next;
if(max -> yuwen > p -> yuwen)
{
n = max -> number;
max -> number = p -> number;
p -> number = n;
strcpy(t,max -> name);
strcpy(max -> name,p -> name);
strcpy(p -> name,t);
chengji = max -> yuwen;
max -> yuwen = p -> yuwen;
p -> yuwen = chengji;
chengji = max -> shuxue;
max -> shuxue = p -> shuxue;
p -> shuxue = chengji;
chengji = max -> yingyu;
max -> yingyu = p -> yingyu;
p -> yingyu = chengji;
}
}
max = head;
p = head;
}
show(head);
break;
case 4:
for(i = 1;i < k; i++)
{
for(j = i+1; j < k; j++)
{
max = p;
p = p -> next;
if(max -> shuxue > p -> shuxue)
{
n = max -> number;
max -> number = p -> number;
p -> number = n;
strcpy(t,max -> name);
strcpy(max -> name,p -> name);
strcpy(p -> name,t);
chengji = max -> yuwen;
max -> yuwen = p -> yuwen;
p -> yuwen = chengji;
chengji = max -> shuxue;
max -> shuxue = p -> shuxue;
p -> shuxue = chengji;
chengji = max -> yingyu;
max -> yingyu = p -> yingyu;
p -> yingyu = chengji;
}
}
max = head;
p = head;
}
show(head);
break;
case 5:
for(i = 1;i < k; i++)
{
for(j = i+1; j < k; j++)
{
max = p;
p = p -> next;
if(max -> yingyu > p -> yingyu)
{
n = max -> number;
max -> number = p -> number;
p -> number = n;
strcpy(t,max -> name);
strcpy(max -> name,p -> name);
strcpy(p -> name,t);
chengji = max -> yuwen;
max -> yuwen = p -> yuwen;
p -> yuwen = chengji;
chengji = max -> shuxue;
max -> shuxue = p -> shuxue;
p -> shuxue = chengji;
chengji = max -> yingyu;
max -> yingyu = p -> yingyu;
p -> yingyu = chengji;
}
}
max = head;
p = head;
}
show(head);
break;
default :
printf("出去错误,请重试.\n");
}
return 0;
}如果在main()里用case 5:rank(head);break;每次选择了一种排序风格后,数据还可以继续使用;但是如果用
case 5:head = rank(head);break;的话,数据使用过一次之后,在选别的类型的排序方式时,就会提示没有任何数据,head == NULL 了,这怎么理解?
[ 本帖最后由 gaochizhen33 于 2010-11-27 21:37 编辑 ]








