插入排序(单链表)

#include<stdio.h>
#include<stdlib.h>
struct arry{
       long num;
       struct arry *next;
       };
main()
{
      char end_input,sort_type;
      long num,crls=0;/*crls用于判断输出是否需要换行*/
      unsigned long p_count=0;/*指针编号计数器*/
      struct arry *head,*cur,*temp,*last;/*t1t2用于输入,f1f2用于查找和排序*/
      head=(struct arry*)malloc(sizeof(struct arry));
      head->next=NULL;
      last=cur=head;
      puts("Please choose a type of sort:U(High-->Low)/D(Low-->High)");
      printf(">>");/*仿Unix输入提示符^_^*/
      scanf("%c",&sort_type);
      getchar();/*吸收多余的回车*/
      while (sort_type!='u'&&sort_type!='U'&&sort_type!='d'&&sort_type!='D')
      {
            puts("Illigal input./nPlease choose a type of sort:U(High-->Low)/D(Low-->High)");
            printf(">>");
            scanf("%c",&sort_type);
            getchar();/*吸收多余的回车*/
      }/*排序类型标识符,U/u降序,D/d升序*/
      do
      {
          temp=(struct arry*)malloc(sizeof(struct arry));
          temp->next=NULL;
          cur->next=temp;
          cur=temp;
          printf(">>");
          scanf("%ld",&cur->num);
          getchar();
          switch (sort_type)
          {
                 case 'u':
                 case 'U':
                      for (temp=head;temp->next!=NULL;temp=temp->next)
                      {
                          if (cur->num>temp->next->num)/*当前数字比表中的大的时候*/
                          {
                              cur->next=temp->next;
                              temp->next=cur;
                              last->next=NULL;
                              cur=last;/*原来的代码有一段是cur->next->next=NULL
                              其实这样做,会把后面的链表给打断,最大的错误*/
                              break; 
                          }
                          if (temp->next->next==NULL)last=cur;
                      }
                      break;
                 case 'd':
                 case 'D':
                      for (temp=head;temp->next!=NULL;temp=temp->next)
                      {
                          if (cur->num<temp->next->num)
                          {
                              cur->next=temp->next;
                              temp->next=cur;
                              last->next=NULL;
                              cur=last;
                              /*原来的错误同上*/
                              break;
                          }
                          if (temp->next->next==NULL)last=cur;/*
                              之前代码说明cur已经接在链尾,这里是更新尾指针*/
                      }
                      break;
          }/*switch语句结束*/
          printf("End input?(Y for exit,other for continue)");
          end_input=getchar();
          getchar();
      }while (end_input!='y'&&end_input!='Y');/*end_input为结束输入标识符*/
      #if 0
      temp=(struct arry*)malloc(sizeof(struct arry));
      cur->next=temp;/*建立前件与新指针的连接*/
      temp->next=NULL;
      cur=temp;/*创立并且指向当前新创立的指针*/
      #endif
      printf("The sorted numbers:");
      for (temp=head;temp->next!=NULL;temp=temp->next)
      {
          if (crls%5==0)putchar('/n');
          printf("%10ld",temp->next->num);
          crls++;
          }
      putchar('/n');
      /*接下来释放开辟的地址*/
      cur=head;
      for (temp=head;cur->next!=NULL;temp=cur)
      {
          printf("Freed address:%X/n",temp);
          cur=temp->next;
          free(temp);
      }
      free(cur);
      printf("Freed address:%X/n/n",temp);
      puts("All allocated memories have been freed.");
      system("pause");
}

彩云 版本:mCloud2.2.0 | 大小:11 MB| 语言:中文 适用固件:Android 2.0及以上 分享日期:2013-12-13 游戏题材:商业 开发商:中国移动通信集团公司 内容介绍 彩云是中国移动旗下的个人网盘产品,面向所有用户提供安全、便捷、高效的个人云存储服务,帮助用户统一存储和管理不同终端的个人信息资产。用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费领取T级空间。同时,依托运营商的资源优势,为用户提供业内速度最优的上传、下载体验。 【功能特点】 * 账号信息,安全无虞!——彩云向所有用户提供运营商级别的安全防护,除了免费登陆提醒,还有文件加密,全方位保障用户数字资产安全。 * T级空间,瞬间领取!——用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费升级T级空间。 *精彩资源,短信分享!——直接转存他/她的彩云里,也可给他/她发短信,分享短信彩云无限量,免费发! * 跨端使用,就用彩云!——彩云为手机、个人电脑、平板电脑等提供的客户端覆盖所有操作系统,文件存彩云,手机、电脑,哪里想用点哪里。 * 手机信息,一键备份!——手机端可以实现通讯录、短彩信、手机应用、手机图片、手机视频和日历所有信息的全备份,信息丢失后只需一键恢复,即可找回所有数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值