简体   繁体   English

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

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

I will first ask the question and then include all code. 我将首先提出问题,然后包括所有代码。 I am working in a data structures course and we have a make file provided us, but my C++ skills are mediocre at best. 我正在上一个数据结构课程,并且提供了一个make文件,但是我的C ++技能充其量只是中等水平。 I have poured over many different articles and I still can't find out how to integrate the code in order to create a new node... I must be able to create new nodes in order to enqueue them, dequeue them, and do other manipulations on them related to a doubly linked list. 我翻阅了许多不同的文章,但仍然找不到如何集成代码以创建新节点的方法。我必须能够创建新节点以使其入队,出队以及执行其他操作。对它们的操作与双向链表有关。

I have tried many different ways of building a constructor but it always seems there is some type of compiler error. 我尝试了许多不同的构建构造函数的方法,但是似乎总是存在某种类型的编译器错误。

I am not allowed to change any code in the main file, but all my work must be contained in a templated header file. 我不允许更改主文件中的任何代码,但是我的所有工作都必须包含在模板化的头文件中。 Can someone show me how to do this correctly? 有人可以告诉我如何正确执行此操作吗? I will now include relevant code. 我现在将包括相关代码。

Here is my header file. 这是我的头文件。 I am aiming to get the buildNode() function in the LinkedList class to create a Node, then I can add the node to a doubly linked list. 我的目标是在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>;
}
};

I will only include the elements of the main file that are important to this particular challenge, which is 我将仅包含对这个特定挑战很重要的主文件元素,即

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.

EDIT: When I use the 编辑:当我使用

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

line in the buildNode() function, I get a linker error stating: 在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

I believe you are looking for this syntax: 我相信您正在寻找以下语法:

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

The template type should accompany the data type name. 模板类型应伴随数据类型名称。

In

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

    Node();

    ~Node();

    void getData() {

    }
};

Node(); says the constructor exists somewhere, but is not defined here. 说构造函数存在于某处,但未在此处定义。 You need to fully implement this function and ~Node(); 您需要完全实现此功能和~Node(); . As an aside, the Rule of Zero recommends that you not have ~Node(); 顺便说一句,零规则建议您不要~Node(); at all. 完全没有

Node() 
{ 
    do stuff here 
} 

is common, but in your case 很常见,但就您而言

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

will work a bit better. 会更好一点。 See Member Initializer List for more details. 有关更多详细信息,请参见成员初始化程序列表

Once you have Node(); 一旦有了Node(); correctly implemented you can create a new node as suggested with 正确实施后,您可以按照以下建议创建一个新节点

Node<T> *n = new Node<T>();

Addendum: 附录:

next(nullptr) and prev(nullptr) are pointing the links to null, a safe parking space for unused pointers. next(nullptr)prev(nullptr)将链接指向null,这是未使用指针的安全停车位。 This makes testing the node to see what it has been linked to much easier than leaving them uninitialized. 与不初始化节点相比,这使测试节点以查看其链接对象要容易得多。 Initializing data in the constructor is probably easiest with 在构造函数中初始化data可能是最简单的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM