@okokme
2018-07-21T15:08:13.000000Z
字数 567
阅读 535
c语言
单链表进行冒泡排序
int count(Node *pHead)
{
Node *temp;
int count = 0;
temp = pHead->next;
if(pHead == NULL || pHead->next == NULL)
return 0; //如果头结点为空,返回0节点
while(temp != NULL)
{
count++;
temp = temp->next;
}
return count; //返回目标节点数
}
Node *ascending_sort(Node *pHead,int count)
{
Node *temp;
while(count--) //外层循环
{
for(temp = pHead;temp->next->next != NULL;temp = temp->next) //内层循环
{
if(temp->next->number > temp->next->next->number) //如果符合交换条件
{
Node *cur = temp->next;
Node *loop = temp->next->next;
cur->next = loop->next;
temp->next = loop;
loop->next = cur;
}
}
}
temp->next->next = NULL;
printf("\t\t\t\tThe data has been sorted!\n");
return pHead;
}