![](/img/trans.png)
[英]I am unable to reverse the given linked list with the following code. Can someone point out the error in the code?
[英]Can someone point out the problem in my linked list implementation?
當我編譯下面的代碼時,出現“head does not name a type”的編譯錯誤。 有人可以解釋出什么問題了嗎?
#include <iostream>
using namespace std;
/* Link list node */
struct node {
int val;
struct node* next;
node(int x)
{
this->val = x;
next = NULL;
}
};
struct node *head = new node(3);
head -> next = new node(4); // error: head does not name a type.
head -> next->next = new node(5); // error: head does not name a type.
void print(){
struct node* temp = head;
while (temp != NULL) {
cout << temp->val << " ";
temp = temp->next;
}
}
int main()
{
print();
return 0;
}
我不知道為什么會收到錯誤消息。 請有人幫助我。
只允許在函數外聲明。 head->next = node(4)
等表達式需要在 function 中。您應該將該代碼移至main()
中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.