我正在寫一個方法來在C++中反轉鏈接列表。我試圖使用Node*
而不是void
返回類型,但面臨一些錯誤。在C++中反轉鏈接列表的方法
我的方法代碼..
Node* Reverse(Node *head)
{
struct node* prev = NULL;
struct node* current = head;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
的編譯時錯誤消息我收到了..
solution.cc: In function 'Node* Reverse(Node*)':
solution.cc:24:22: error: cannot convert 'Node*' to 'Reverse(Node*)::node*' in initialization
node* current = head;
^
solution.cc:28:24: error: invalid use of incomplete type 'struct Reverse(Node*)::node'
next = current->next;
^
solution.cc:23:14: error: forward declaration of 'struct Reverse(Node*)::node'
struct node* prev = NULL;
^
solution.cc:29:16: error: invalid use of incomplete type 'struct Reverse(Node*)::node'
current->next = prev;
^
solution.cc:23:14: error: forward declaration of 'struct Reverse(Node*)::node'
struct node* prev = NULL;
^
solution.cc:33:10: error: cannot convert 'Reverse(Node*)::node*' to 'Node*' in assignment
head = prev;
^
solution.cc:34:1: error: no return statement in function returning non-void [-Werror=return-type]
}
^
cc1plus: some warnings being treated as errors
「節點」和「節點」的定義是什麼? –
1問題是你必須在這個方法之前沒有完全聲明結構節點,你可能只需要放置一個類似struct節點的前向變量;請包括完整的聲明。你也說你的函數會返回類型o Node *,但是你不返回任何東西。 –
你不需要在任何地方輸入'struct'。 – Zsolt