创建包含节点的链接列表C++
Creating a LinkedList with Nodes C++
我的任务是在C++中创建一个链表。我应该为链接列表和节点创建一个结构。在这个程序中我应该有很多功能,但为了我自己的理解,我现在只是试图编写一个附加函数。
我正在使用 3 个文件:
HW10.H
#ifndef Structures_hw10
#define Structures_hw10
#include <iostream>
struct Node{
int value;
Node* next;
};
struct LinkedList{
Node* head = NULL;
};
void append(int);
#endif
HW10.cpp
#include "hw10.h"
void LinkedList::append(int data){
Node* cur = head;
Node* tmp = new Node;
tmp->value = data;
tmp->next = NULL;
if(cur->next == NULL) {
head = tmp;
}
else {
while(cur->next != NULL){
cur = cur->next;
}
cur->next = tmp;
}
// delete cur;
}
主.cpp
#include "hw10.h"
int main(){
LinkedList LL;
LL.append(5);
LL.append(6);
Node* cur = LL.head;
while(cur->next != NULL){
std::cout<<cur->value<<std::endl;
cur = cur->next;
}
return 0;
}
为了编译这段代码,我在终端中输入:
g++ -o hw10 hw10.cpp main.cpp
这是我收到的回复:
In file included from main.cpp:2:0:
hw10.h:13:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
In file included from hw10.cpp:1:0:
hw10.h:13:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
hw10.cpp: In function 'void append(int)':
hw10.cpp:10:15: error: 'head' was not declared in this scope
我的主要函数应该创建一个新的链表并附加 2 个新节点,并打印出它们的值(以确保它有效(。
在你的结构声明中,你必须像这样在结构内附加;
struct LinkedList{
Node* head = NULL;
void append(int);
};
尝试添加"-std=c++11"以消除警告。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题