【发布时间】:2015-12-14 17:08:24
【问题描述】:
我有这门课:
class Elem
{
public:
int x;
Elem *nast;
};
我有一个默认构造函数,函数显示x。我做了十个元素列表,但是如何对这个按x排序的列表进行排序?
我试过了:
void Sortlinked_list(Elem *head)
{
int ile = 0;
Elem *cur;
cur = head;
while( cur->nast != NULL )
{
cur = cur->nast;
ile++;
}
Elem* curr = head;
Elem* next;
int temp;
for(int i = 0; i < ile; i++ )
{
while( curr && curr->nast )
{
next = curr->nast;
while (next)
{
if (curr->show() > next->show())
{
std::swap(next->nast, curr->nast);
}
next = next->nast;
}
curr = curr->nast;
}
}
}
但它不起作用。输出为:http://i.stack.imgur.com/vJrRK.png
如果有人可以帮助我解决这个问题?我花了 3 个小时,一无所获。
【问题讨论】:
-
从未听说过大黄蜂排序...
-
@SergeyA see en.wikipedia.org/wiki/Bubble_sort 你指的是错字吗?
-
是的,冒泡排序 xD 错误
-
@dekros 您可以编辑您的问题以使其更清晰。
-
我猜大黄蜂排序的大部分条目都按正确的顺序排列,大约 95%,无论如何。老兄
标签: c++ sorting linked-list bubble-sort