如何在双链接列表中创建新节点?(C++)

How do I create a new Node in a Doubly Linked List? (C++)

本文关键字:C++ 节点 新节点 创建 链接 列表      更新时间:2023-10-16

我将首先提出问题,然后包括所有代码。我正在学习一门数据结构课程,我们提供了一个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 */ 
}