鱼C论坛

 找回密码
 立即注册
查看: 1675|回复: 2

[已解决]链表冒泡排序思想

[复制链接]
发表于 2017-6-12 00:49:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. #include"string.h"
  4. struct student
  5. {
  6.         int num;
  7.         char name[19];
  8.         int age;
  9.         struct student *next;
  10. };
  11. struct student *Create(void)
  12. {
  13.         int num;
  14.         struct student *head=NULL,*p,*last;
  15.         FILE *fp;
  16.         fp=fopen("e:\\a.txt","r");
  17.         fscanf(fp,"%d",&num);
  18.         while(num!=0)
  19.         {
  20.                 p=(struct student *)malloc(sizeof(struct student));
  21.                 p->next=NULL;
  22.                 if(head==NULL)
  23.                 {
  24.                         head=p;
  25.                         last=p;
  26.                 }
  27.                 else
  28.                 {
  29.                         last->next=p;
  30.                         last=p;
  31.                 }
  32.                 p->num=num;
  33.                 fscanf(fp,"%s%d%d",p->name,&p->age,&num);
  34.         }
  35.         fclose(fp);
  36.         return head;
  37. }
  38. struct student *PaiXu(struct student *head)
  39. {
  40.         struct student *p=head,*q,*r,*s,*t,*m,*n=NULL;
  41.         while(head!=n)
  42.         {
  43.                 q=head;
  44.                 r=q->next;
  45.                 while(r!=NULL)
  46.                 {
  47.                         if(r->next==n)
  48.                         {
  49.                                 n=r;
  50.                                 if(q->next > r->next)
  51.                                 {
  52.                                         s->next=r;
  53.                                         r->next=q;
  54.                                         m=r;
  55.                                         r=q;
  56.                                         q=m;
  57.                                 }
  58.                                 break;
  59.                         }
  60.                         else
  61.                         {
  62.                                 t=r->next;
  63.                                 if(q->num > r->num)
  64.                                 {
  65.                                         if(q==head)
  66.                                         {
  67.                                                 r->next=q;
  68.                                                 q->next=t;
  69.                                                 head=r;
  70.                                                 m=r;
  71.                                                 r=q;
  72.                                                 q=m;
  73.                                                 head=q;
  74.                                         }
  75.                                         else
  76.                                         {
  77.                                                 s->next=r;
  78.                                                 r->next=q;
  79.                                                 q->next=t;
  80.                                                 m=r;
  81.                                                 r=q;
  82.                                                 q=m;
  83.                                         }
  84.                                 }
  85.                                 s=q;
  86.                                 q=r;
  87.                                 r=r->next;
  88.                         }
  89.                 }
  90.         }
  91.         return head;
  92. }
  93. void OutPut2(struct student *head)
  94. {
  95.         struct student *p=head;
  96.         while(p!=NULL)
  97.         {
  98.                 printf("%d        %s        %d\n",p->num,p->name,p->age);
  99.                 p=p->next;
  100.         }
  101.         printf("********************\n");
  102. }
  103. int main()
  104. {
  105.         int age;
  106.         char name[19];
  107.         head=Create();
  108.         OutPut2(head);
  109.         head=PaiXu(head);
  110.         OutPut2(head);
  111.         return 0;
  112. }
复制代码
最佳答案
2017-6-12 12:29:25
你这个直接把链表之间的连接给打乱了,我写了一个应该是对的,你到时候把文件路径给改一下。有很多变量没有使用,代码有一些小错误。最好不要用拼音,我看的不是很习惯。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct student
  5. {
  6.     int num;
  7.     char name[19];
  8.     int age;
  9.     struct student *next;
  10. };
  11. void exchage_str(char (*a)[19],char (*b)[19])
  12. {
  13.     char temp[19]="zhutianci";
  14.     strcpy(temp,*a);
  15.     strcpy(*a,*b);
  16.     strcpy(*b,temp);
  17. }
  18. struct student *Create(void)
  19. {
  20.     int num;
  21.     struct student *head=NULL,*p,*last = NULL;
  22.     FILE *fp;
  23.     fp=fopen("a.txt","r");
  24.     fscanf(fp,"%d",&num);
  25.     while(num!=0)
  26.     {
  27.         p=(struct student *)malloc(sizeof(struct student));
  28.         p->next=NULL;
  29.         if(head==NULL)
  30.         {
  31.             head=p;
  32.             last=p;
  33.         }
  34.         else
  35.         {
  36.             last->next=p;
  37.             last=p;
  38.         }
  39.         p->num=num;
  40.         fscanf(fp,"%s%d%d",p->name,&p->age,&num);
  41.     }
  42.     fclose(fp);
  43.     return head;
  44. }
  45. struct student *sort(struct student *head)
  46. {
  47.     struct student *i,*j;
  48.     struct student temp;
  49.     for(i=head;i!=NULL;i=i->next)
  50.         for(j=head;j!=NULL;j=j->next)
  51.         {
  52.             if(i->num>j->num)
  53.             {
  54.                 temp.age=i->age;
  55.                 i->age=j->age;
  56.                 j->age=temp.age;
  57.                 exchage_str(&(i->name),&(j->name));
  58.                 temp.num=i->num;
  59.                 i->num=j->num;
  60.                 j->num=temp.num;
  61.             }
  62.         }
  63.     return head;
  64. }
  65. void OutPut2(struct student *head)
  66. {
  67.     struct student *p=head;
  68.     while(p!=NULL)
  69.     {
  70.         printf("%d        %s        %d\n",p->num,p->name,p->age);
  71.         p=p->next;
  72.     }
  73.     printf("********************\n");
  74. }
  75. int main()
  76. {
  77.     struct student *head;
  78.     head=Create();
  79.     OutPut2(head);
  80.     head=sort(head);
  81.     OutPut2(head);
  82.     return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-6-12 00:50:08 | 显示全部楼层
请问大佬们一下,这个哪里错了??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-12 12:29:25 | 显示全部楼层    本楼为最佳答案   
你这个直接把链表之间的连接给打乱了,我写了一个应该是对的,你到时候把文件路径给改一下。有很多变量没有使用,代码有一些小错误。最好不要用拼音,我看的不是很习惯。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct student
  5. {
  6.     int num;
  7.     char name[19];
  8.     int age;
  9.     struct student *next;
  10. };
  11. void exchage_str(char (*a)[19],char (*b)[19])
  12. {
  13.     char temp[19]="zhutianci";
  14.     strcpy(temp,*a);
  15.     strcpy(*a,*b);
  16.     strcpy(*b,temp);
  17. }
  18. struct student *Create(void)
  19. {
  20.     int num;
  21.     struct student *head=NULL,*p,*last = NULL;
  22.     FILE *fp;
  23.     fp=fopen("a.txt","r");
  24.     fscanf(fp,"%d",&num);
  25.     while(num!=0)
  26.     {
  27.         p=(struct student *)malloc(sizeof(struct student));
  28.         p->next=NULL;
  29.         if(head==NULL)
  30.         {
  31.             head=p;
  32.             last=p;
  33.         }
  34.         else
  35.         {
  36.             last->next=p;
  37.             last=p;
  38.         }
  39.         p->num=num;
  40.         fscanf(fp,"%s%d%d",p->name,&p->age,&num);
  41.     }
  42.     fclose(fp);
  43.     return head;
  44. }
  45. struct student *sort(struct student *head)
  46. {
  47.     struct student *i,*j;
  48.     struct student temp;
  49.     for(i=head;i!=NULL;i=i->next)
  50.         for(j=head;j!=NULL;j=j->next)
  51.         {
  52.             if(i->num>j->num)
  53.             {
  54.                 temp.age=i->age;
  55.                 i->age=j->age;
  56.                 j->age=temp.age;
  57.                 exchage_str(&(i->name),&(j->name));
  58.                 temp.num=i->num;
  59.                 i->num=j->num;
  60.                 j->num=temp.num;
  61.             }
  62.         }
  63.     return head;
  64. }
  65. void OutPut2(struct student *head)
  66. {
  67.     struct student *p=head;
  68.     while(p!=NULL)
  69.     {
  70.         printf("%d        %s        %d\n",p->num,p->name,p->age);
  71.         p=p->next;
  72.     }
  73.     printf("********************\n");
  74. }
  75. int main()
  76. {
  77.     struct student *head;
  78.     head=Create();
  79.     OutPut2(head);
  80.     head=sort(head);
  81.     OutPut2(head);
  82.     return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 01:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表