实现思路:
先遍历一遍单链表,将最大的数放到链表的最后,第二次遍历链表时,将N-1个数里最大的放在N-1的位置,依次循环遍历,直到只剩下两个结点;
具体代码实现如下:
typedef int DataType;
typedef struct SListNode{
struct SListNode* _next;
DataType _data;
}SListNode,*pSListNode;
void BubbleSortList(pSListNode* pHead)
{
pSListNode cur = NULL;
pSListNode tmp = NULL;
pSListNode tail = NULL;
if (*pHead == NULL || (*pHead)->_next == NULL)
return;
while ((*pHead)->_next != tail)
{
int flag = 0;
cur = *pHead;
tmp = cur->_next;
while (tmp != tail)
{
if ((cur->_data) > (tmp->_data))
{
DataType temp = cur->_data;
cur->_data = tmp->_data;
tmp->_data = temp;
flag = 1;
}
cur = tmp;
tmp = tmp->_next;
}
if (flag == 0)
{
break;
}
tail = cur;
}
}