关于链表的插入法排序
#include <stdio.h>
#include <stdlib.h>
typedef struct _list
{
int elem;
struct _list *pPre;
struct _list *pNext;
}List;
List *CreatList()//创建一个双线链表
{
List *pHead = (List*)malloc(sizeof(List));
pHead->pNext = pHead->pPre = pHead;
return pHead;
}
void InsertList(List *pHead,int elem)//头插法插入数据函数
{
List *pCur = pHead,*pTemp = pCur->pNext;
List *pNew = (List*)malloc(sizeof(List));
pNew->elem = elem;//数据初始化
//以下是插入过程
pNew->pNext = pCur->pNext;
pCur->pNext = pNew;
pNew->pPre = pTemp->pPre;
pTemp->pPre = pNew;
}
void Print(List *pHead)//打印链表函数
{
List *pCurrent = pHead->pNext;
while(pCurrent!=pHead)
{
printf("%d,",pCurrent->elem);
pCurrent = pCurrent->pNext;
}
}
void main()
{
List *pHead = CreatList();
List *pCur = pHead->pNext,*pPrev,*pT=pHead;
int data[] = {1,32,4,3,24,2,5},count = 0,i = 0;
count = sizeof(data)/sizeof(data[0]);
for(i=0;i<count;i++)
{
InsertList(pHead,data[i]);
}
//下面是插入法排序的算法,
while(pCur->pNext!=pHead)
{
pPrev = pCur;
for(pT=pPrev->pPre;pT!=pHead;pT=pT->pPre)
{
if(pT->elem <= pPrev->elem)
{
break;
}
}
pPrev->pNext = pT->pNext;
pT->pNext = pPrev;
pPrev->pPre = pT->pNext->pPre;
pT->pNext->pPre = pPrev;
}
Print(pHead);
}我觉得插入法的算法是对的,但就是不能运行,为什么??求大神解决,另外那个单向链表的插入法的算法该如何解决??