最近重新学习C语言,用到了链表排序,自己写完后记录一下,本文使用的是类似冒泡排序,具体其他算法可以修改sortList实现。
结构体定义:
typedef struct Node_Test_My
{
int data;
Node_Test_My* next;
}Node,*pNode;
进行初始化:
pNode initListByInput()
{
int input;
int result;
printf("please input numbers, end with nan:");
result = scanf("%d",&input);
if (0 == result)
{
return NULL;
}
pNode head = (pNode)malloc(sizeof(Node));
if (NULL == head)
{
return NULL;
}
head->next = NULL;//生成头结点省得后面加入节点的时候进行判断
pNode p = head;
while (0 != result)//当输入的不是数字时,返回值为0,input没有赋值成功,作为退出条件
{
pNode temp = (pNode)malloc(sizeof(Node));
if (NULL == temp)
{
freeList(head);
return NULL;
}
temp->data = input;
temp->next = p->next;
p->next = temp;
p = temp;
result = scanf("%d",&input);
}
p = head->next;
free(head);//释放头结点
return p;
}
链表生成之后进行排序:
pNode sortListByHead(pNode head) { if (NULL == head) { return NULL; } pNode myHead = (pNode)malloc(sizeof(Node)); if (NULL == myHead) { return head; } myHead->next = head;//生成一个新的头指针,指向原来的头结点,省去多余的判断 pNode p = head; pNode q = head->next; pNode preP = myHead;//p节点的前一个节点 pNode preQ = p; //q节点的前一个节点 pNode temp; while (NULL != p) { preQ = p; //记录每次q节点的前一个节点 q = p->next;//每次循环将q指针指向p的下一个节点 while (NULL != q) { if (p->data > q->data) { //前指针的移动(互换) preP->next = q; preQ->next = p; //后指针的移动(互换) temp = q->next; q->next = p->next; p->next = temp; //指针指向调换,保证p和q的在链表中的位置不变 temp = p; p = q; q= temp; } preQ = q; //记录q的前一个节点 q = q->next;//q指向下一个 } preP = p; p = p->next; } head = myHead->next;//自定义头结点的下一个节点为真正的头结点 free(myHead); //释放自定义的头结点 return head; //返回头结点 }
排序前后进行链表输出:void printList(pNode head) { pNode p = head; while (p != NULL) { if (p != head) { printf("->"); } printf("%d",p->data); p = p->next; } printf("\n"); }
最后进行链表申请空间的释放:
void freeList(pNode head) { pNode p = head; pNode q; while (p != NULL) { q = p->next; free(p); p = q; } }
打完收工~~~