2

I wrote a code of getting a linked list with numbers, and trying to make the list as ascending series. Unfortunately the code is not complied and I don't know why.

I have tried to play with the pointers and references but I cant put my hand on what is wrong.

#include <iostream>
using namespace std;

class ListNode {
public:
  ListNode(const int &info) : data(info), nextPtr(0) {}

  int getData() const { return data; }
  ListNode *getNext() const { return nextPtr; }
  void setNext(ListNode *next) { nextPtr = next; }

private:
  int data;
  ListNode *nextPtr;
};

ListNode sort(ListNode &temp) {
  ListNode *first = &temp;
  ListNode *curr = first;
  ListNode *next = curr->getNext();
  ListNode *found = 0;

  while (curr->getNext() != 0) {
    if (curr->getData() > next->getData()) {
      if (curr == first) {
        first = next;
        found = curr;
      }

      else {
        curr->setNext(next->getNext());
        found = next;
      }

      break;
    }

    curr = next;
    next = next->getNext();
  }

  curr = first;
  next = curr->getNext();

  while (curr->getNext() != 0) {
    if (curr->getData() <= found->getData() &&
        found->getData() < next->getData()) {
      curr->setNext(found);
      found->setNext(next);
      break;
    }

    curr = next;
    next = next->getNext();
  }

  return *first;
}

void print(ListNode &temp) {
  ListNode *curr = &temp;

  while (curr != 0) {
    cout << curr->getData() << " ";
    curr = curr->getNext();
  }

  cout << endl;
}

int main1() {
  ListNode a(2);
  ListNode b(5);
  ListNode c(8);
  ListNode d(13);
  ListNode e(18);
  ListNode f(7);
  ListNode g(21);

  a.setNext(&b);
  b.setNext(&c);
  c.setNext(&d);
  d.setNext(&e);
  e.setNext(&f);
  f.setNext(&g);

  print(a);
  print(sort(a));

  return 0;
}

I have checked hundred times and do not know why this code is not compiling.

5
  • What error do you get? Commented Jul 9, 2019 at 17:12
  • E0461, and C2664 Commented Jul 9, 2019 at 17:17
  • Possible duplicate of Invalid initialization of non-const reference of type Commented Jul 9, 2019 at 17:21
  • E0461, and C2664 Next time please add the exact text of the error message. Most users will not know these codes without having to google. You can get the text of the error message in the Output Tab of Visual Studio. And yes I mean Output Tab and not the errors list. The output tab is in a more verbose format and also it is in plain text. Commented Jul 9, 2019 at 17:26
  • actually i am new here i barely succeed open a question forum. haha. thank you Commented Jul 9, 2019 at 18:34

2 Answers 2

1

sort() should return a pointer to the node, so return first instead of *first and change the return type to ListNode*. Then change print(sort(a)) to print(*sort(a)). See it run here: http://coliru.stacked-crooked.com/a/c3e72983e83f6914

Sign up to request clarification or add additional context in comments.

3 Comments

@LORINECHERRY Glad I could help. And just to let you know we normally don't use references to nodes but instead pointers, and the functions that work on linked lists usually access those nodes through pointers. So everywhere you are accessing a node it should be through a pointer to that node, not a reference like what you're doing in your code. For example, the print function should take a ListNode* instead of a ListNode&.
and why is that?
@LORINECHERRY It allows you to allocate and deallocate memory dynamically. There are also no such thing as "null" references. So if you have a function called countNodes(Node&) it's basically saying that there has to be at least one node in the list, because there's no way to pass an empty list, which is not very intuitive.
0
     #include<iostream>
    using namespace std;

class ListNode
{
public:
    ListNode(const int &info) :data(info), nextPtr(0)
    {
    }

    int getData() const
    {
        return data;
    }
    ListNode * getNext() const
    {
        return nextPtr;
    }
    void setNext(ListNode * next)
    {
        nextPtr = next;
    }
private:
    int data;
    ListNode *nextPtr;
};

ListNode sort(ListNode &temp)
{
    ListNode *first = &temp;
    ListNode *curr = first;
    ListNode *next = curr->getNext();
    ListNode *found = 0;

    while (curr->getNext() != 0)
    {
        if (curr->getData() > next->getData())
        {
            if (curr == first)
            {
                first = next;
                found = curr;
            }

            else
            {
                curr->setNext(next->getNext());
                found = next;
            }

            break;
        }

        curr = next;
        next = next->getNext();
    }

    curr = first;
    next = curr->getNext();

    while (curr->getNext() != 0)
    {
        if (curr->getData() <= found->getData() && found->getData() < next->getData())
        {
            curr->setNext(found);
            found->setNext(next);
            break;
        }

        curr = next;
        next = next->getNext();
    }

    return *first;
}

You are passing a temporary(rvalue) to a function which expects an lvalue

    void print(const ListNode &temp)
{


const ListNode * curr = &temp;

    while (curr != 0)
    {
        cout << curr->getData() << " ";
        curr = curr->getNext();
    }

    cout << endl;
}
//I am expecting main() here , I think this is what you meant.

int main()
{
    ListNode a(2);
    ListNode b(5);
    ListNode c(8);
    ListNode d(13);
    ListNode e(18);
    ListNode f(7);
    ListNode g(21);

    a.setNext(&b);
    b.setNext(&c);
    c.setNext(&d);
    d.setNext(&e);
    e.setNext(&f);
    f.setNext(&g);

    print(a);
    print(sort(a));

    return 0;
    }

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.