C语言高手请帮个忙

来源:百度知道 编辑:UC知道 时间:2024/05/11 20:01:42
帮我看下SORT1函数算法错在哪里.其它函数都没问题,这里不解决,下面的程序没心情去做啊!各位,拜托了!
#include<stdio.h>
#include<string.h>
#include<alloc.h>
#define LEN sizeof(struct stu)
struct stu
{int num;
char name[20];
char sex[5];
int score[10];
struct stu *next;};
int n=0;
struct stu *new(int i,int j)
{int a,b=1,flag;
struct stu *head,*p1,*p2;
head=p1=p2=NULL;
do
{n+=1;
flag=0;
p1=(struct stu *)malloc(LEN);
printf("Input stu%d ID:",b++);
scanf("%d",&p1->num);
printf("Input name:");
scanf("%s",p1->name);
printf("Input sex(boy or girl):");
loop:
scanf("%s",p1->sex);
if(strcmp(p1->sex,"boy")==0||strcmp(p1->sex,"girl")==0)flag=1;
if(flag==0)
{printf("Input error,input sex again:");
goto loop;}
printf("Input sc

问题出在你只交换了p2和p1的指针的值
这是没有意义的
if(p1->num>p2->num)
{p0=p2;p2=p1;p1=p0;}
我们的目的是交换p2和p1所指向的节点
所以
把p0=p2;p2=p1;p1=p0;
改为*p0=*p2;*p2=*p1;*p1=*p0;

就ok啦^-^

这么长呀,你也发到 www.dayi.net 试试。

嗯。初学者常犯的错误。

我很无聊