| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1615 人关注过本帖
标题:这个链表的排序哪里有问题? sort函数有问题
取消只看楼主 加入收藏
万士心平
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2010-1-30
结帖率:55.56%
收藏
已结贴  问题点数:20 回复次数:0 
这个链表的排序哪里有问题? sort函数有问题
#include<stdio.h>
#include<stdlib.h>
struct stu  //声明学生信息结构体
{
    char name[20];
    int num;
    float score;
    struct stu *next;
};
main()
{    struct stu *sort(struct stu *p); //声明排序函数
    struct stu *create(struct stu *p,int n);
    struct stu *head,*p;
    head=(struct stu *)malloc(sizeof(struct stu));
    int n;  //n是学生数目
    printf("请输入学生数目");
    scanf("%d",&n);
    while(n>=100)
    {
    scanf("%d",&n);
    }
    head=create(head,n);  //创建链表
    head=sort(head);    //按照什么排序
    p=head->next;
    while(p)
    {
        printf("%5s %5d %.3f",p->name,p->num,p->score);
        p=p->next;
    }
}
struct stu *create(struct stu *p,int n)
{   struct stu *pp,*pt;
    pt=p;
    int i;
    for(i=0;i<n;i++)
    {    pp=(struct stu *)malloc(sizeof(struct stu));
        printf("请输入学生姓名:");   
        scanf("%s",pp->name);
        printf("请输入学生学号:");
        scanf("%d",&pp->num);
        printf("请输入学生成绩:");
        scanf("%f",&pp->score);
        pt->next=pp;
        pt=pp;
    /*    if(i==n-1)
        {
            p->next=NULL;
            
        }
        else
        {    p=(struct stu *)malloc(sizeof(struct stu));
            pt->next=p;
            pt=p;
        
        }
   
      */

    }
    pp->next=NULL;
return p;

}
struct stu *sort(struct stu *p)  //排序,返回一个指针
{
    struct stu *first,*cursor,*max,*prev; //prev用来跟踪max
    first=p;
    prev=first;
    p=p->next;
    while(p)
    {
    for(max=cursor=p;cursor;cursor=cursor->next)  //找到最大的
   
    {
        if(cursor->score>max->score)
        {
            max=cursor;
            break;
        }
    }
    while(prev->next!=max)  //找到max
    {
    prev=prev->next;
    }
    prev->next=max->next;
    max->next=p->next; //把max和p连接起来
    p->next=max;
    p=p->next;  //p向后移,减少比较范围
    prev=first;
    }
    return first;

}
搜索更多相关主题的帖子: 链表 sort 函数 
2010-07-16 18:15
快速回复:这个链表的排序哪里有问题? sort函数有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011652 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved