【问题标题】:Code does not compile - linked lists, sort list to ascending order代码不编译 - 链表,按升序排序列表
【发布时间】:2019-07-09 17:09:02
【问题描述】:

我编写了一个获取带有数字的链表的代码,并尝试将列表设为升序系列。不幸的是,代码没有被遵守,我不知道为什么。

我尝试过使用指针和引用,但我无法确定哪里出了问题。

#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;
}

我检查了一百次,不知道为什么这段代码没有编译。

【问题讨论】:

  • 你得到什么错误?
  • E0461 和 C2664
  • E0461 和 C2664 下次请添加错误消息的确切文本。大多数用户无需谷歌就不会知道这些代码。您可以在 Visual Studio 的“输出”选项卡中获取错误消息的文本。是的,我的意思是输出选项卡,而不是错误列表。输出选项卡的格式更详细,而且是纯文本。
  • 其实我是新来的,我几乎没有成功打开一个问题论坛。哈哈。谢谢

标签: c++ linked-list


【解决方案1】:

sort() 应该返回一个指向节点的指针,所以返回 first 而不是 *first 并将返回类型更改为 ListNode*。然后将print(sort(a)) 更改为print(*sort(a))。在这里运行:http://coliru.stacked-crooked.com/a/c3e72983e83f6914

【讨论】:

  • @LORINECHERRY 很高兴我能帮上忙。只是为了让您知道,我们通常不使用对节点的引用,而是使用 pointers,并且在链表上工作的函数通常通过指针访问这些节点。因此,无论您在何处访问节点,都应该通过指向该节点的指针,而不是像您在代码中所做的那样的引用。例如,打印函数应采用ListNode* 而不是ListNode&amp;
  • 为什么会这样?
  • @LORINECHERRY 它允许您动态分配和释放内存。也没有“空”引用之类的东西。因此,如果您有一个名为countNodes(Node&amp;) 的函数,它基本上是说列表中必须至少有一个节点,因为没有办法传递一个空列表,这不是很直观。
【解决方案2】:
     #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(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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多