我正在研究链表的bubblesort算法。它正在移动数据,但它并没有对数据进行适当的排序。现在我只担心整合的链表。问题在哪里?非常感谢。
void List::linkedListBubbleSort()
{
bool swap = true;
Node * temp = firstNode;
Node * current;
if(firstNode == 0)
{
cout << "List is empty." << endl;
return;
}
else
{
while(swap == true)
{
for(current = firstNode; current != NULL && current->next != NULL; current = current->next)
{
if(current->data > current->next->data)
{
swap = true;
temp->data = current->data;
current->data = current->next->data;
current->next->data = temp->data;
}
else
swap = false;
}
}
答案 0 :(得分:0)
你不必使用while(swap == true),因为在某些时候if(current-&gt; data&gt; current-&gt; next-&gt; data)返回false然后你的上层循环while(swap == true) )不会让你进一步迭代。
else
{
for(current = firstNode; current != NULL && current->next != NULL; current = current->next)
{
if(current->data > current->next->data)
{
temp->data = current->data;
current->data = current->next->data;
current->next->data = temp->data;
}
}
}
答案 1 :(得分:0)