qsort排序指针数组:
#include<stdlib.h>
#include<stdio.h>
typedef struct student_t{
int num;
struct student_t *pNext;
}Student_t,*pStudent_t;
int comparePointer(const void* pleft,const void *pright){
pStudent_t* p1=(pStudent_t*)pleft;
pStudent_t* p2=(pStudent_t*)pright;
return (*p1)->num-(*p2)->num;
}
#define N 10
int main(){
int i,listLen=0;
pStudent_t phead,ptail,pNew,pCur;
pStudent_t *pArr;
phead=ptail=NULL;
while(scanf("%d",&i)!=EOF){
pNew=(pStudent_t)calloc(1,sizeof(Student_t));
pNew->num=i;
if (NULL==phead)
{
phead=pNew;
ptail=pNew;
}else
{
pNew->pNext=phead;
phead=pNew;
}
listLen++;
}
pArr=(pStudent_t*)calloc(listLen,sizeof(pStudent_t));
pCur=phead;
for ( i = 0; i < listLen; i++)
{
pArr[i]=pCur;
pCur=pCur->pNext;
}
qsort(pArr,listLen,sizeof(pStudent_t),comparePointer);
printf("链表排序后的结点值:\n");
for ( i = 0; i < listLen; i++)
{
printf("%3d",pArr[i]->num);
}
printf("\n");
printf("链表本身的结点值(通过头插法建立的链表):\n");
pCur=phead;
while (pCur!=NULL)
{
printf("%3d",pCur->num);
pCur=pCur->pNext;
}
printf("\n");
while (phead!=NULL)
{
pCur=phead;
phead=phead->pNext;
free(pCur);
pCur=NULL;
}
ptail=NULL;
free(pArr);
system("pause");
}