/********************************************************************
* 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 ----------------------------------------------------*/