C语言中链表的插入及排序

/********************************************************************
* File Name  : insert_sort.c          *
* Created   : 2007/05/08                                            *
* Author   :    SunYonggao                                          *
* Description  :  插入排序的实现 非递减排序             *
/*    如果是顺序表的化可以用二份插入排序提高效率
*********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//定义类型 所有的排序例子中都是用的int作为data
typedef int elemType;     

//返回值
#define RET_SUCCESS   ( 1  ) 
#define RET_FAILED   ( 0  )  

//定义链表的长度
#define  LIST_MAX_SIZE      (10)

//定义链表申请内存不够时报错信息
#define NO_MEMORY   printf("Error! Not enough memory!/n");exit(1)

//结构体定义
typedef struct tagNode_t
{
 elemType data;     
 struct tagNode_t * pstNext; 
}node_t;

// 初始化链表 链表在外部定义 node_t * pstHead
// 调用时要用 initList(&pstHead)的形式 注意头节点的内容为空仅仅为了操作方便
// 随机产生0-LIST_MAX_SIZE的数字作为链表的data
int initList(node_t ** pstHead)
{
 int iRet = RET_SUCCESS;
 int iCir = 0;
 node_t * pstTmp1 = NULL;
 node_t * pstTmp2 = NULL;
 
 //初始化头节点
 * pstHead = (node_t *)malloc(sizeof(node_t));
 if ( !pstHead )
 {
  NO_MEMORY;
 }
 pstTmp1 = * pstHead;
 //链表初始化
 srand( time(NULL) );//随机数
    for( iCir = 0; iCir < LIST_MAX_SIZE; iCir++ )
 {
  pstTmp2 = (node_t *)malloc(sizeof(node_t));
  if ( !pstTmp2 )
  {
   NO_MEMORY;
  }
  //赋初值
  pstTmp2->data = rand() % LIST_MAX_SIZE;
  pstTmp2->pstNext = NULL;
  pstTmp1->pstNext = pstTmp2;
  pstTmp1 = pstTmp2;
 } 
 return iRet;
}

// 打印链表 链表的data元素是可打印的整形    
int showList(node_t * pstHead)
{
 int iRet = RET_SUCCESS;
 node_t * pstTmp = pstHead->pstNext;
 if ( NULL == pstHead )
 {
  printf("链表的头节点是空/n");
  iRet = RET_FAILED;
 }
 else
 {
  while ( pstTmp )
  {
   printf("%d ", pstTmp->data);
   pstTmp = pstTmp->pstNext;
  }
  printf("/n");
 }
 return iRet;
}

/* 删除包括头节点在内的所有的节点. 07/04/28  */
int destroyList(node_t * pstHead)
{
 node_t * pstTmp = NULL;   /* Temp pointer for circle  */
 int iRet = RET_SUCCESS;
 if ( !pstHead )
 {
  printf("Error! pstHead is null/n");
  iRet = RET_FAILED;
 }
 else
 {
  while ( pstHead )  /* Free  nodes      */
  {
   pstTmp = pstHead;
   pstHead = pstHead->pstNext;
   free(pstTmp);
  }
  pstHead = NULL;
 }
 return iRet;
}/* End of destroyList----------------------------------------------*/
//插入排序
void insert_sort(node_t * pstHead)
{
 node_t * pstToSort = NULL;
 node_t * pstTmpHead = NULL;
 node_t * pstTmp = NULL;

 if ( !pstHead )
 {
  printf("错误!头节点为空./n");
  exit(1);
 }
 if ( pstHead->pstNext && pstHead->pstNext->pstNext )//如果只有一个元素则不用排序
 {
  pstToSort = pstHead->pstNext->pstNext;//要排序的部分的头节点 第一个不用排序
  pstTmpHead = pstHead;
  pstTmpHead->pstNext->pstNext = NULL; //将第一个元素先挂上
  while ( pstToSort )
  {
   while ( pstTmpHead->pstNext && pstToSort->data > pstTmpHead->pstNext->data )
   {
    pstTmpHead = pstTmpHead->pstNext;
   }
   pstTmp = pstToSort; //将带排序的最前面的节点取出
   pstToSort = pstToSort->pstNext; //带排序链表头节点后移
   pstTmp->pstNext = pstTmpHead->pstNext;//插入pstTmp
   pstTmpHead->pstNext = pstTmp;
   pstTmpHead = pstHead;//pstTmpHead指向头节点进行新一轮插入
  }
 }
}

//入口函数
void main()
{
 node_t * pstHead = NULL;
 initList(&pstHead);//初始化链表
 printf("pai qian /n");
 showList(pstHead);//打印元素
 insert_sort(pstHead);//插入排序
 printf("pai hou/n");
 showList(pstHead);
 destroyList(pstHead);
 pstHead = NULL;
}
/* End of file ----------------------------------------------------*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值