| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1384 人关注过本帖
标题:关于链表排序的问题
只看楼主 加入收藏
OnlyZhu
Rank: 1
等 级:新手上路
帖 子:34
专家分:4
注 册:2015-11-10
结帖率:77.78%
收藏
已结贴  问题点数:20 回复次数:3 
关于链表排序的问题
图片附件: 游客没有浏览图片的权限,请 登录注册
这个程序最后是把连接好的两个链表按照num的值排了序,但是我的这个代码没有排序,我试着排了序但排不出来,所以下面这个代码没有排序的代码,还请大神教教我
程序代码:
#include<stdio.h>
#include<malloc.h>

#define LEN sizeof(struct student)

int sum = 0;
/* User Code Begin(考生可在本行后添加代码,定义程序中使用的结构体类型、声明自定义函数的原型,行数不限) */
struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *creat(void);
struct student *merge(struct student *ahead, struct student *bhead);

/* User Code End(考生添加代码结束) */

/* print以规定的格式完成遍历显示指定的链表 */
void print(struct student *Head);

int main(void)
{
    struct student *ah, *bh, *ac;
    
    printf("创建链表A,请输入学号及成绩(均输入为0时表示停止):\n");
    ah = creat();
    printf("\n创建链表B,请输入学号及成绩(均输入为0时表示停止):\n");
    bh = creat();

    printf("\n链表A:");
    print(ah);
    printf("\n链表B:");
    print(bh);
    
    ac = merge(ah, bh);
    printf("\n两个链表共有%d个人\n链表C:", sum);
    print(ac);
    
    return 0;
}

void print(struct student *Head)
{
    while (Head != NULL)
    {
        printf("%d,%d  ", Head->num, Head->score);
        Head = Head->next;
    }
}

/* User Code Begin(考生在此后完成自定义函数的设计,行数不限) */
struct student *creat(void)
{
    struct student *head;
    struct student *p1, *p2;
    int num = 0;
    
    sum++;
    printf("学生 %d: ", sum);
    p1 = p2 = (struct student *)malloc(LEN);
    scanf("%d %d", &p1->num, &p1->score);
    head = NULL;
    while (p1->num != 0)
    {
        num++;
        sum++;
        if (num == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
            p2 = p1;
        }
        printf("学生 %d: ", sum);
        p1 = (struct student *)malloc(LEN);
        scanf("%d %d", &p1->num, &p1->score);
    }
    p2->next = NULL;
    sum--;

    return head;
}

struct student *merge(struct student *ahead, struct student *bhead)
{
    struct student *p1,*p2=NULL;
    int i=0;
    
    p1 = ahead;
    if (sum == 0)
    {
        return NULL;
    }
    else if (ahead == NULL)
    {
        return bhead;
    }
    else if (bhead == NULL)
    {
        return ahead;
    }
    else
    {
        while (p1->next != NULL)
        {
            p1 = p1->next;
        }
        p1->next = bhead;
    }
    
    return ahead;
}
2016-04-29 17:49
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:7 
用冒泡排序即可,很简单的,just do it!

未佩好剑,转身便已是江湖
2016-04-29 18:10
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:7 
链表排序帮你写好了

程序代码:
#include<stdio.h>
#include<malloc.h>

#define LEN sizeof(struct student)

int sum = 0;
/* User Code Begin(考生可在本行后添加代码,定义程序中使用的结构体类型、声明自定义函数的原型,行数不限) */
struct student

 {
     int num;
     int score;
     struct student *next;

 };

struct student *creat(void);
struct student *merge(struct student *ahead, struct student *bhead);

/* User Code End(考生添加代码结束) */

/* print以规定的格式完成遍历显示指定的链表 */
void print(struct student *Head);

struct student *partion(struct student *a){

 struct student *b,*c,*d,*pre;

 int i,j,corenum=0;

 b=a->next;

 while(b!=NULL){
    corenum++;
    b=b->next;

 }

 //printf("%d",corenum);


 b=a->next;


 for(i=0;i<corenum-1;i++){
  b=a->next;
  pre=a;
  for(j=corenum-i-1;j;j--){
   if(b->num>b->next->num){
    c=b->next;
    b->next=b->next->next;
    c->next=b;
    pre->next=c;
    pre=c;
   }
   else{
    pre=b;
    b=b->next;

   }
  }

 }

  return a;
}

int main(void)

 {
     struct student *ah, *bh, *ac;

     printf("创建链表A,请输入学号及成绩(均输入为0时表示停止):\n");
     ah = creat();
     printf("\n创建链表B,请输入学号及成绩(均输入为0时表示停止):\n");
     bh = creat();

     printf("\n链表A:");
     print(ah);
     printf("\n链表B:");
     print(bh);

     ah=partion(ah);
     bh=partion(bh);
     ac = merge(ah, bh);
     printf("\n两个链表共有%d个人\n链表C:", sum);
     print(ac);

     return 0;

 }

void print(struct student *Head)

 {
     struct student *Head1;
     Head1=Head->next;
     while (Head1 != NULL)
     {
         printf("%d,%d  ", Head1->num, Head1->score);
         Head1 = Head1->next;
     }


 }

/* User Code Begin(考生在此后完成自定义函数的设计,行数不限) */
struct student *creat(void)

 {
     struct student *head;
     struct student *p1, *p2;
     int num = 0;

     head= (struct student *)malloc(LEN);

     sum++;
     printf("学生 %d: ", sum);
     p1 = p2 = (struct student *)malloc(LEN);
     scanf("%d %d", &p1->num, &p1->score);

     //head = NULL;
     while (p1->num != 0)
     {
         num++;
         sum++;

         if (num == 1)
         {
             head ->next= p1;
         }
         else
         {
             p2->next = p1;
             p2 = p1;
         }
         printf("学生 %d: ", sum);
         p1 = (struct student *)malloc(LEN);
         scanf("%d %d", &p1->num, &p1->score);
     }
     p2->next = NULL;
     sum--;

     return head;

 }

struct student *merge(struct student *ahead, struct student *bhead)

 {
     struct student *p1,*p2,*chead,*p3;


     p1 = ahead;
     chead= (struct student *)malloc(LEN);
     chead->next=NULL;
     p3=chead;
     p1=ahead->next;
     p2=bhead->next;
     while((ahead->next!=NULL)&&(bhead->next!=NULL)){
        if(p1->num<p2->num){
            ahead->next=p1->next;
            p3->next=p1;
            p3=p1;
            p1=ahead->next;

        }
        else{
            bhead->next=p2->next;
            p3->next=p2;
            p3=p2;
            p2=bhead->next;

        }
     }
     if(ahead->next!=NULL) p3->next=ahead->next;
     else p3->next=bhead->next;


     return chead;

 }

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2016-04-29 23:54
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:7 
程序代码:
#include<stdio.h>
#include<malloc.h>

#define LEN sizeof(struct student)

int sum = 0;
/* User Code Begin(考生可在本行后添加代码,定义程序中使用的结构体类型、声明自定义函数的原型,行数不限) */
struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *creat(void);
struct student *sort(struct student *ahead);
struct student *merge(struct student *ahead, struct student *bhead);

/* User Code End(考生添加代码结束) */

/* print以规定的格式完成遍历显示指定的链表 */
void print(struct student *Head);

int main(void)
{
    struct student *ah, *bh, *ac;
    
    printf("创建链表A,请输入学号及成绩(均输入为0时表示停止):\n");
    ah = creat();
    printf("\n创建链表B,请输入学号及成绩(均输入为0时表示停止):\n");
    bh = creat();

    printf("\n链表A:");
    print(ah);
    printf("\n链表B:");
    print(bh);
    
    ac = merge(ah, bh);
    printf("\n两个链表共有%d个人\n链表C:", sum);
    print(ac);
    printf("\n开始排序。。。");
    ac=sort(ac);
    printf("\n排序后:");
    print(ac);
   
    return 0;
}

 student *sort(struct student *ac){
     struct student *h=(struct student*)malloc(sizeof(struct student));
     h->next=ac; 
    for(struct student *p=h->next,*pr=h,*pp=p;p;){
        pr=h;
        while(pr->next!=p)pr=pr->next;
        pp=pp->next;
        for(struct student *q=h->next,*qr=h;q!=p;q=q->next,qr=qr->next){
            if(p->num<q->num){
                pr->next=p->next;
                p->next=q; 
                qr->next=p;
                break; 
            }
        } 
        p=pp;
    }
    return h->next;
}
void print(struct student *Head)
{
    while (Head != NULL)
    {
        printf("%d,%d  ", Head->num, Head->score);
        Head = Head->next;
    }
}

/* User Code Begin(考生在此后完成自定义函数的设计,行数不限) */
struct student *creat(void)
{
    struct student *head;
    struct student *p1, *p2;
    int num = 0;
    
    sum++;
    printf("学生 %d: ", sum);
    p1 = p2 = (struct student *)malloc(LEN);
    scanf("%d %d", &p1->num, &p1->score);
    head = NULL;
    while (p1->num != 0)
    {
        num++;
        sum++;
        if (num == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
            p2 = p1;
        }
        printf("学生 %d: ", sum);
        p1 = (struct student *)malloc(LEN);
        scanf("%d %d", &p1->num, &p1->score);
    }
    p2->next = NULL;
    sum--;

    return head;
}

struct student *merge(struct student *ahead, struct student *bhead)
{
    struct student *p1,*p2=NULL;
    int i=0;
    
    p1 = ahead;
    if (sum == 0)
    {
        return NULL;
    }
    else if (ahead == NULL)
    {
        return bhead;
    }
    else if (bhead == NULL)
    {
        return ahead;
    }
    else
    {
        while (p1->next != NULL)
        {
            p1 = p1->next;
        }
        p1->next = bhead;
    }
    
    return ahead;
}

剑栈风樯各苦辛,别时冰雪到时春
2016-04-30 09:56
快速回复:关于链表排序的问题
数据加载中...
 
   



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

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