Node* sort(Node* phead)
{
if (NULL==phead || NULL==phead->pNext)
{
return phead;
}
Node* ptemp=phead;
Node* plastchangepoint=NULL;//标记上一次
while (plastchangepoint!=phead->pNext)
{
bool changed=false;
for (ptemp=phead;ptemp->pNext->pNext!=plastchangepoint;ptemp=ptemp->pNext)
{
if (ptemp->value>ptemp->pNext->value)
{
int t=ptemp->value;
ptemp->value=ptemp->pNext->value;
ptemp->pNext->value=t;
changed=true;
}
}
if (!changed)
{
break;
}
plastchangepoint=ptemp->pNext;
}
return phead;
}写正确函数需要注意的地方:链表的及时终止冒泡排序
最新推荐文章于 2025-08-04 23:26:50 发布
本文介绍了一种基于链表的数据结构进行排序的算法实现。通过遍历链表并使用冒泡排序的思想来调整节点顺序,确保链表中的元素按升序排列。此方法简单直观,适用于链表基本操作的学习。

1351

被折叠的 条评论
为什么被折叠?



