读书人

这个程序如何不排序啊请大家帮忙

发布时间: 2012-02-12 17:16:33 作者: rapoo

这个程序怎么不排序啊,请大家帮忙
我今年考程序员。学习中。但是下面这个程序不排序是怎么回事。我都调试好几天了。谢谢大家。
/*动态输入学生信息并且排序*/

#include "stdio.h "
#include "stdlib.h "
#include "string.h "
#define null 0


struct student
{
int num;
char name[10];
struct student *next;
};


void main()
{
static n=0;
int i,j;
struct student newstd;
struct student *t;
struct student *head,*p,*s;
printf( "\t\t\t\t学生信息表\n ");
head=null;
head=(struct student *)malloc(sizeof(struct student));/*为头节点分配空间*/
if(head==null)
{
printf( "分配空间失败!!!\07\n ");
return;
}
head-> next=null;
head-> num=0;
p=head;
//循环输入多个学生的信息
do{
printf( "输入学号(输入0结束): ");
scanf( "%d ",&newstd.num);
if(newstd.num==0)
{
break;
}
printf( "输入姓名: ");
scanf( "%s ",newstd.name);
s=(struct student *)malloc(sizeof(struct student));/*分配新空间*/
if(s==null)
{
printf( "分配空间失败!!!\07\n ");
break;
}
strcpy(s-> name,newstd.name);
s-> num=newstd.num;
s-> next=null;
p-> next=s;
p=s;
n=n+1;
}while(1);
//开始排序
s=head-> next;/*指针初始化*/
p=s-> next;
for(i=0;i <=n-2;i++)
{
for(j=n;j> i;j--)
{
if(s-> num> p-> num)
{
t-> num=s-> num;
strcpy(t-> name,s-> name);
s-> num=p-> num;
strcpy(s-> name,p-> name);
p-> num=t-> num;
strcpy(p-> name,t-> name);
}//if
}//for j
}//for i
p=head-> next;
while(p!=null)
{
printf( "学号%d,姓名%s\n ",p-> num,p-> name);
p=p-> next;
}//while
}//main
//这个程序并没有进行排序




[解决办法]
你跟踪一下s和p这两个指针没有指向你希望指向的值
[解决办法]
/*修改了一下,可以了*/
/*你的程序中有几点问题
1.t这个指针没有初始化,即没有指向某个内存空间就使用了。
2.在排序时,只考虑了循环,没有考虑p,s的偏移
3.对于t,因为是临时用于变量的交换,所以在使用后不要忘记释放。
*/

#include "stdio.h "
#include "stdlib.h "
#include "string.h "
#define null 0


struct student
{
int num;
char name[10];
struct student *next;
};


void main()
{
static n=0;
int i,j;
struct student newstd;
struct student *t = (struct student *)malloc(sizeof(struct student));
struct student *head,*p,*s;
printf( "\t\t\t\t学生信息表\n ");
head=null;
head=(struct student *)malloc(sizeof(struct student));/*为头节点分配空间*/
if(head==null)
{
printf( "分配空间失败!!!\07\n ");
return;
}
head-> next=null;
head-> num=0;
p=head;
//循环输入多个学生的信息
do{
printf( "输入学号(输入0结束): ");
scanf( "%d ",&newstd.num);
if(newstd.num==0)
{
break;
}
printf( "输入姓名: ");
scanf( "%s ",newstd.name);
s=(struct student *)malloc(sizeof(struct student));/*分配新空间*/
if(s==null)
{
printf( "分配空间失败!!!\07\n ");
break;
}
strcpy(s-> name,newstd.name);
s-> num=newstd.num;
s-> next=null;
p-> next=s;
p=s;
n=n+1;
}while(1);
//开始排序
s=head-> next;/*指针初始化*/
p=s-> next;
for(i=0;i <=n-2;i++)
{
for(j=n;j> i;j--)
{
if(s-> num> p-> num)
{
t-> num=s-> num;
strcpy(t-> name,s-> name);
s-> num=p-> num;
strcpy(s-> name,p-> name);
p-> num=t-> num;
strcpy(p-> name,t-> name);
}//if
p = p-> next;
if(p == NULL)
{
break;
}
}//for j
s = s-> next;
p = s-> next;
}//for i
p=head-> next;
while(p!=null)
{
printf( "学号%d,姓名%s\n ",p-> num,p-> name);
p=p-> next;
}//while
free(t);
}//main

[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define null 0


struct student
{
int num;
char name[10];
struct student *next;
};


void main()
{
static n=0;
// int i,j;
struct student newstd;
struct student *t;
struct student *head,*p,*s;
printf( "\t\t\t\t学生信息表\n ");
head=null;
head=t=(struct student *)malloc(sizeof(struct student));/*为头节点分配空间*/
if(head==null)
{
printf( "分配空间失败!!!\07\n ");
return;
}
head-> next=null;
head-> num=0;
p=head;
//循环输入多个学生的信息
do{
printf( "输入学号(输入0结束): ");
scanf( "%d ",&newstd.num);
if(newstd.num==0)
{
break;
}
printf( "输入姓名: ");
scanf( "%s ",newstd.name);
s=(struct student *)malloc(sizeof(struct student));/*分配新空间*/
if(s==null)
{
printf( "分配空间失败!!!\07\n ");
break;
}
strcpy(s-> name,newstd.name);


s-> num=newstd.num;
s-> next=null;
p-> next=s;
p=s;
n=n+1;
}while(1);
//开始排序
s=head-> next;/*指针初始化*/
// p=s-> next;
/* for(i=0;i <=n-2;i++)
{
for(j=n-1;j> i;j--)
{
if((s+j)> (p+j)-> num)
{
(t+j)=(s+j);

(s+j)=(p+j);

(p+j)=(t+j);

}//if
}//for j
}//for i */
while ( s != NULL )
{
p=s-> next;
while ( p != NULL )
{
if ( s-> num > p-> num )
{
t-> num = s-> num;
s-> num = p-> num;
p-> num = t-> num;
}
p = p-> next;
}
s = s-> next;
}
p=head-> next;
while(p!=null)
{
printf( "学号%d,姓名%s\n ",p-> num,p-> name);
p=p-> next;
}//while
}//main

读书人网 >C语言

热点推荐