如何在双链接列表中创建新节点?(C++)
How do I create a new Node in a Doubly Linked List? (C++)
我将首先提出问题,然后包括所有代码。我正在学习一门数据结构课程,我们提供了一个make文件,但我的C++技能充其量只能算是平庸。我已经写了很多不同的文章,但我仍然找不到如何集成代码来创建一个新的节点。。。我必须能够创建新的节点,以便将它们入队、出队,并对它们进行与双链表相关的其他操作。
我尝试了许多不同的方法来构建构造函数,但似乎总是存在某种类型的编译器错误。
我不允许更改主文件中的任何代码,但我所有的工作都必须包含在模板化的头文件中。有人能告诉我如何正确地做这件事吗?我现在将包括相关代码。
这是我的头文件。我的目标是在LinkedList类中获得buildNode()函数来创建一个Node,然后我可以将该节点添加到一个双链表中。
#include <limits>
#include <string>
#include <cassert>
#include <iostream>
template <typename T>
class Node {
public:
T data;
Node* next;
Node* prev;
Node();
~Node();
void getData() {
}
};
template <typename T>
class Iterator {
private:
public:
Iterator() {
}
int operator*() const {
}
Iterator& operator++() {
}
bool operator==(Iterator const& rhs) {
}
bool operator!=(Iterator const& rhs) {
}
};
template <typename T>
class LinkedList {
private:
Node<T> *head = 0;
Node<T> *tail = 0;
public:
LinkedList() {
}
~LinkedList() {}
Iterator<T> begin() const {
}
Iterator<T> end() const {
}
bool isEmpty() const {
if (this->head == 0) {
std::cout << "Isempty works.";
return true;
}
}
T getFront() const {
}
T getBack() const {
}
void enqueue (T element) {
}
void dequeue() {
}
void pop() {
}
void clear() {
}
bool contains(int element) const {
}
void remove(int element) {
}
void buildNode() { //experimental function.
Node<T> n;
// Node n = new Node();
// Node<T> n;
//Node<T> *n = new Node<T>();
//Node n = new Node<T>;
}
};
我将只包括主文件中对这个特定挑战很重要的元素,即
int main()
{
// Get ready.
LinkedList<string&> referenceList;
LinkedList<char const*> valueList;
//ascribe valueList and referenceList to a variable inside the LinkedList class...
unsigned int numOfStrings = 8;
string testStrings[] = {
"alpha"
, "bravo"
, "charlie"
, "charlie"
, "dog"
, "echo"
, "foxtrot"
, "golf"
};
string tempStr;
// Test isEmpty function.
assert(valueList.isEmpty() && referenceList.isEmpty());
referenceList.buildNode(); //can't get this to work. Later the methodology will be transported to enqueue method.
编辑:当我使用时
Node<T> * n = new Node<T>;
在buildNode()函数的第行中,我得到一个链接器错误,声明:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/Boisselle/Library/Caches/CLion2016.2/cmake/generated/LinkedList-75be2cc8/75be2cc8/Debug --target LinkedList -- -j 8
Scanning dependencies of target LinkedList
[ 50%] Building CXX object CMakeFiles/LinkedList.dir/main.cpp.o
[100%] Linking CXX executable LinkedList
Undefined symbols for architecture x86_64:
"Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>::Node()", referenced from:
LinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>::buildNode() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [LinkedList] Error 1
make[2]: *** [CMakeFiles/LinkedList.dir/all] Error 2
make[1]: *** [CMakeFiles/LinkedList.dir/rule] Error 2
make: *** [LinkedList] Error 2
我相信您正在寻找以下语法:
Node<T> * node_pointer = new Node<T>;
模板类型应与数据类型名称一起使用。
在中
template <typename T>
class Node {
public:
T data;
Node* next;
Node* prev;
Node();
~Node();
void getData() {
}
};
Node();
表示构造函数存在于某个地方,但这里没有定义。您需要完全实现此功能和~Node();
。顺便说一句,零规则建议您根本没有~Node();
。
Node()
{
do stuff here
}
是常见的,但在您的情况下是
Node() : next(nullptr), prev(nullptr)
{
/* does nothing */
}
效果会好一点。有关详细信息,请参阅成员初始化程序列表。
一旦正确实现了Node();
,就可以按照的建议创建一个新节点
Node<T> *n = new Node<T>();
附录:
next(nullptr)
和prev(nullptr)
将链接指向null,这是未使用指针的安全停车位。这使得测试节点以查看它链接到了什么比让它们未初始化要容易得多。使用在构造函数中初始化data
可能最简单
Node(T newdata) : data(newdata), next(nullptr), prev(nullptr)
{
/* does nothing */
}
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 引擎节点:未定义的符号:_ZTV6Config
- 为什么"delete"关键字不删除节点?
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何按数字顺序插入链表节点?
- 为什么我的双向链表删除函数会删除多个节点?
- 编写一个函数来删除单链表中的节点(尾部除外),仅授予对该节点的访问权限