我遇到了這段代碼的問題。我很確定它正在交換。泡沫排序鏈接列表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