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

时间:2017-01-06 17:42:57

标签: c++ doubly-linked-list

我先问问题,然后再包含所有代码。我正在从事数据结构课程,我们为我们提供了一个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

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找这种语法:

Node<T> * node_pointer = new Node<T>;

模板类型应与数据类型名称一致。

答案 1 :(得分:0)

template <typename T>
class Node {
public:
    T data;
    Node* next;
    Node* prev;

    Node();

    ~Node();

    void getData() {

    }
};

Node();表示构造函数存在于某处,但未在此处定义。您需要完全实现此功能并~Node();。另外,the Rule of Zero建议您根本没有~Node();

Node() 
{ 
    do stuff here 
} 

很常见,但在你的情况下

Node() : next(nullptr), prev(nullptr) 
{ 
    /* does nothing */ 
} 

会更好一些。有关详细信息,请参阅Member Initializer List

正确实施Node();后,您可以按照

的建议创建新节点
Node<T> *n = new Node<T>();

附录:

next(nullptr)prev(nullptr)将链接指向null,这是一个未使用指针的安全停车位。这使得测试节点以查看链接的内容比将它们保持未初始化要容易得多。使用

初始化构造函数中的data可能是最简单的
Node(T newdata) : data(newdata), next(nullptr), prev(nullptr) 
{ 
    /* does nothing */ 
}