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.

David G
  • 94,763
  • 41
  • 167
  • 253
  • What error do you get? – David G Jul 09 '19 at 17:12
  • E0461, and C2664 – Lorin Sherry Jul 09 '19 at 17:17
  • Possible duplicate of [Invalid initialization of non-const reference of type](https://stackoverflow.com/questions/27172033/invalid-initialization-of-non-const-reference-of-type) – Abhijeet Kumar Srivastava Jul 09 '19 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. – drescherjm Jul 09 '19 at 17:26
  • actually i am new here i barely succeed open a question forum. haha. thank you – Lorin Sherry Jul 09 '19 at 18:34

2 Answers2

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

David G
  • 94,763
  • 41
  • 167
  • 253
  • @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&`. – David G Jul 09 '19 at 17:33
  • and why is that? – Lorin Sherry Jul 09 '19 at 17:45
  • @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. – David G Jul 09 '19 at 17:51
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;
    }