C++ 我编写此程序是为了反转链表中的元素,编译此程序后,将显示反向错误()。为什么?
我编写此程序是为了反转链表中的元素,编译此程序后,将显示反向错误()。为什么? 错误显示:- ||在函数“void reverse()”中:| |40 |错误:“运算符=”不匹配(操作数类型为“Node”和“long int”)| |40 |注:候选人为:|C++ 我编写此程序是为了反转链表中的元素,编译此程序后,将显示反向错误()。为什么?,c++,C++,我编写此程序是为了反转链表中的元素,编译此程序后,将显示反向错误()。为什么? 错误显示:- ||在函数“void reverse()”中:| |40 |错误:“运算符=”不匹配(操作数类型为“Node”和“long int”)| |40 |注:候选人为:| #include<iostream> using namespace std; struct Node { int data; Node* next; }; Node* head; void insert
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* head;
void insert(int data)
{
Node* newNode = new Node(); // create a new node
newNode->data = data;
newNode->next = NULL;
if(head == NULL ){ //if head is null, that means this is your first node
head = newNode; //so update the value of head
}else{
Node* temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
void print()
{
Node* temp = head;
while(temp!=NULL)
{
cout<<" "<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void reverse()
{
Node* current;
Node* prev;
Node* nextaddress;
current = head;
prev = NULL;
while(current!=NULL)
{
nextaddress = current->next;
current->next = prev;
prev = current;
current = nextaddress;
}
head = prev;
}
int main()
{
head = NULL;
int a, n;
cout<<"\n enter the number of elements to be stored into the list :";
cin>>n;
cout<<"\n enter those elements :";
for(int i=1; i<=n; i++)
{
cin>>a;
insert(a);
}
print();
cout<<"\n the reverse of the linkedlist is :";
reverse();
print();
return 0;
}
#包括
使用名称空间std;
结构体类型
{
int数据;
节点*下一步;
};
节点*头;
无效插入(整型数据)
{
Node*newNode=newNode();//创建一个新节点
新建节点->数据=数据;
newNode->next=NULL;
如果(head==NULL){//如果head为NULL,则表示这是您的第一个节点
head=newNode;//因此更新head的值
}否则{
节点*温度=头部;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
temp->next=newNode;
}
}
作废打印()
{
节点*温度=头部;
while(temp!=NULL)
{
cout在C/C++中,您需要在作为指针的每个变量名之前放置一个*
。因此在reverse()
中,第一行应为:
Node*current、*prev、*nextaddress;//prev和nextaddress成为节点*
在C/C++中,您需要在作为指针的每个变量名之前放置一个*
。因此在reverse()
中,第一行应为:
Node*current、*prev、*nextaddress;//prev和nextaddress成为Node*
这就是为什么每个编码标准每行说一个变量
你写的:
Node* current , prev, nextaddress;
current = head;
prev = NULL;
你的意思是:
Node* current , * prev, * nextaddress;
// ^^ ^^ Without the star they are Node objects.
current = head;
prev = NULL;
您应该键入的内容是:
Node* current = head;
Node* prev = nullptr;
Node* nextaddress;
嘿,看,它不会占用更多的空间。这就是为什么每个编码标准都说每行一个变量
你写的:
Node* current , prev, nextaddress;
current = head;
prev = NULL;
你的意思是:
Node* current , * prev, * nextaddress;
// ^^ ^^ Without the star they are Node objects.
current = head;
prev = NULL;
您应该键入的内容是:
Node* current = head;
Node* prev = nullptr;
Node* nextaddress;
嘿,看,它不会占用更多的空间。你真的读了错误消息吗?你检查了错误所在的行了吗?更重要的是,你知道,例如,Node*current,prev,nextaddress;
只有declarescurrent
作为指针,而不是其他两个变量?这个问题当然可以通过bi来解决t、 但是对于正在学习的人来说,这是一个合法的编程错误。我不同意在这里关闭是合适的。你真的阅读了错误消息吗?你检查了错误所在的行了吗?更重要的是,你知道,例如,Node*current,prev,nextadress;
只将declarescurrent
作为指针,而不是另外两个变量?这个问题当然可以澄清一点,但对于正在学习的人来说,这是一个合理的编程错误。我不同意在这里结束是合适的。