[英]Code does not compile - linked lists, sort list to ascending order
我寫了一個代碼來獲取一個帶有數字的鏈表,並試圖將列表作為升序序列。 不幸的是,代碼沒有得到遵守,我不知道為什么。
我曾嘗試使用指針和引用,但我無法解決問題所在。
#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;
}
我已經檢查了數百次,不知道為什么這段代碼沒有編譯。
sort()
應該返回一個指向節點的指針,因此返回first
而不是*first
並將返回類型更改為ListNode*
。 然后將print(sort(a))
更改為print(*sort(a))
。 看到它在這里運行: http : //coliru.stacked-crooked.com/a/c3e72983e83f6914
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.