【发布时间】: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