0

I'm having problems with this code. I'm pretty sure it's in the swapping.

The line: curr->Data() = nextEl.Data() gives me the following error:

"expression must be a modifiable lvalue"

Any help is appreciated. Thank you in advance. Here is the code for my bubble-sort algorithm:

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;
}
4
  • 1
    The code you show doesn't even include the line you say is causing the error. Commented Jul 19, 2012 at 22:30
  • Yeah it's because i tried fixing it, but lines of this sort would go in the if statement in the second nested for loop where swapping occurs. Commented Jul 19, 2012 at 23:24
  • possible duplicate of How to sort a linked list using bubble-sort? Commented Feb 12, 2015 at 15:00
  • 1
    @malat this thread is 2 years old, the thread you linked is 1 year old... so technically that one is a duplicate of mine Commented Feb 17, 2015 at 21:20

3 Answers 3

1

You are doing it wrong. You cannot change the value of temp variable returned by a function.

But you can make it work this way..

int& Data() {return data;};

though this is not good practise. Instead just use the setter you have..

curr->Set(nextEl->Data());
Sign up to request clarification or add additional context in comments.

4 Comments

I just tried this, and its giving me errors, for "curr" it gives me "expression must be of modifiable lvalue" and for "nextEL" it gives me "expression must have class type"
Did you add & the symbol as shown? Anyway, the second form should still work.
yes i did, and thanks, i get no errors now! But i think my looping is wrong... i get a .exe problem
@user1539252- If you hover your mouse below the votes, you'll see a checkmark.. click that.
0

The statement

curr->Data() = nextEl.Data();

will never work, you are trying to assign something to the return value of a function. I don't know how you defined Node, but you probably meant something like

curr->Data = nextEl.Data();

i.e., assign something to a member of Node.

3 Comments

curr->Set(nextEl.Data()), apparently
Note that assigning to the return value of a function can work if the function returns a reference. Whether that's appropriate is an entirely different matter.
ups, i didn't see the definition of Node was right there :) Yes, you are right Ben.
0

change

curr ->Data() = nextEl ->Data(); 
nextEl ->Data() = temp;

to

curr->Set(nextEl ->Data()); 
nextEl->Set(temp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.