新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → C语言中的单链表冒牌排序 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4170 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C语言中的单链表冒牌排序 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     葛靖青001 美女呀,离线,快来找我吧!水瓶座1984-2-14
      
      
      等级:大三(研究MFC有点眉目了!)
      文章:168
      积分:595
      门派:XML.ORG.CN
      注册:2010/11/2

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给葛靖青001发送一个短消息 把葛靖青001加入好友 查看葛靖青001的个人资料 搜索葛靖青001在『 C/C++编程思想 』的所有贴子 点击这里发送电邮给葛靖青001 引用回复这个贴子 回复这个贴子 查看葛靖青001的博客楼主
    发贴心情 C语言中的单链表冒牌排序

    【转自互联网】

    今天做链表排序有个误区,就是以为交换的时候要连next节点也交换,还要固定head节点,想了很久也没做出来,但是后来看网上的提示,才知道只要交换节点内的数据就可以了,根本不用交换next节点

      01 #include <stdio.h>

      02 #include <stdlib.h>

      03

      04 struct node

      05 {

      06     int data;

      07     struct node *next;

      08 };

      09

      10 struct node *create_list(int a[],int len)

      11 {

      12     struct node *phead;

      13     struct node *ptr;

      14     struct node *pre;

      15     phead=(struct node *)malloc(sizeof(struct node));

      16     int i=0;

      17     phead->data=a[i];

      18     phead->next=NULL;

      19     ptr=phead->next;

      20     pre=phead;

      21     for(i=1;i<len;i++)

      22     {

      23         ptr=(struct node *)malloc(sizeof(struct node));

      24         ptr->data=a[i];

      25         ptr->next=NULL;

      26         pre->next=ptr;

      27         ptr=ptr->next;

      28         pre=pre->next;

      29     }

      30

      31     return phead;

      32 }

      33

      34 void print_list(struct node *phead)

      35 {

      36     struct node *ptr=phead;

      37

      38     while(ptr != NULL)

      39     {

      40         printf("%d ",ptr->data);

      41         ptr=ptr->next;

      42     }

      43

      44     printf("\n");

      45 }

      46

      47 struct node *bubble(struct node *phead,int len)

      48 {

      49     struct node *ptr,*next;

      50     int temp;

      51

      52     for(int i=0;i<len;i++)

      53     {

      54         ptr=phead;

      55         next=ptr->next;

      56         for(int j=len-i-1;j>0;j--)

      57         {

      58             if(ptr->data > next->data)

      59             {

      60                 temp=ptr->data;

      61                 ptr->data=next->data;

      62                 next->data=temp;

      63             }

      64             ptr=ptr->next;

      65             next=next->next;

      66         }

      67     }

      68

      69     return phead;

      70 }

      71

      72 int main()

      73 {

      74     int a[10]={

      75         5,3,6,8,9,6,5,4,2,7

      76     };

      77

      78     struct node *phead;

      79     phead=create_list(a,10);

      80

      81     print_list(phead);

      82

      83     phead=bubble(phead,10);

      84

      85     print_list(phead);

      86 }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    ---人之所以能,是相信能!!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/12/7 15:27:00
     
     GoogleAdSense水瓶座1984-2-14
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 点击这里发送电邮给Google AdSense 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 14:37:18

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    3,355.469ms