我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误.为什么
I wrote this program to reverse the elements in a linked list, after compiling this program this is showing error in the reverse (). why?
我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误。为什么?
错误显示:-在函数' void reverse() '中:|错误:没有匹配' operator= '(操作数类型为' Node '和' long int ')||40|注:候选项为:|
#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;
}
这就是为什么每个编码标准都规定每行一个变量。
你写了什么:
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;
看,它不占空间了
在C/c++中,您需要在每个指针变量名之前放置一个*
。所以在reverse()
中,第一行应该是:
Node* current, *prev, *nextaddress; // prev and nextaddress become Node *
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- C ++程序编译错误,找不到/访问文件
- C++程序编译没有问题,但无法运行
- Mongdb C++ 驱动程序编译错误 for document{}.
- 添加类型名会导致程序编译失败
- 将 C 程序更改为 C++ 程序 - 编译错误
- 程序编译,但当分解为函数时实际上不会移动电机
- 如何链接 glib-2.0 库进行 c/c++ 程序编译
- 为什么 gcc 和 clang 为我的程序编译为不同的"const"结果?
- 复数程序编译时的计算方法错误
- qt 命令行应用程序编译
- 程序编译和运行,但一段时间后,它停止使用Typedef关键字在CPP上工作
- QML应用程序编译一个 *form.ui.qml文件,但忽略了关联的.qml文件
- 为什么该程序编译但不显示任何输出
- 分割故障程序编译为共享库,但不静态
- 程序编译,但我认为开关被忽略
- 程序编译后崩溃
- 什么是程序编译中的PIC级别
- 尽管使用不存在的成员,但程序编译
- C++程序编译在 Ubuntu 中失败,但在 MacOSX 中工作