我遇到了这段代码的问题。我很确定它正在交换。泡沫排序链接列表C++
行:curr->Data() = nextEl.Data()
使我有以下错误:
"expression must be a modifiable lvalue"
任何帮助表示赞赏。先谢谢你。 这里是我的冒泡排序算法的代码:
class Node
{
private:
int data;
Node* next;
public:
Node() {};
void Set(int d) { data = d;};
void NextNum(Node* n) { next = n;};
int Data() {return data;};
Node* Next() {return next;};
};
class LinkedList
{
Node *head;
public:
LinkedList() {head = NULL;};
virtual ~LinkedList() {};
void Print();
void AddToTail(int data);
void SortNodes();
};
void LinkedList::SortNodes()
{
Node *curr = head;
Node *nextEl = curr ->Next();
Node *temp = NULL;
if(curr == NULL)
cout <<"There is nothing to sort..."<< endl;
else if(curr -> Next() == NULL)
cout << curr -> Data() << " - " << "NULL" << endl;
else
{
for(bool swap = true; swap;)
{
swap = false;
for(curr; curr != NULL; curr = curr ->Next())
{
if(curr ->Data() > nextEl ->Data())
{
temp = curr ->Data();
curr ->Data() = nextEl ->Data();
nextEl ->Data() = temp;
swap = true;
}
nextEl = nextEl ->Next();
}
}
}
curr = head;
do
{
cout << curr -> Data() << " - ";
curr = curr -> Next();
}
while (curr != NULL);
cout <<"NULL"<< endl;
}
告诉你,即使不包括你说行代码导致错误。 – 2012-07-19 22:30:22
是的,这是因为我试图修复它,但是这种类型的行会在第二个嵌套for循环中的if语句中进行交换。 – philr 2012-07-19 23:24:23
可能重复的[如何使用冒泡排序对链表进行排序?](http://stackoverflow.com/questions/19522121/how-to-sort-a-linked-list-using-bubble-sort) – malat 2015-02-12 15:00:40