与节点的链表

时间:2019-03-09 19:27:17

标签: c++ linked-list nodes

我试图制作一个链表,并使用节点在c ++中对其进行测试。我创建了六个节点,然后像这样向前和向后打印它们:

main.cpp

#include "LinkedList.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void TestAddHead();

int main()
{
    TestAddHead();

    system("pause");

    return 0;
}

void TestAddHead()
{
    cout << "Testing AddHead()" << endl;
    LinkedList<int> data;
    for (int i = 0; i < 12; i += 2)
        data.AddHead(i);
    cout << "Node count: " << data.NodeCount() << endl;
    cout << "Print list forward:" << endl;
    data.PrintForward();
    cout << "Print list in reverse:" << endl;
    data.PrintReverse();
}

LinkedList.h

#pragma once
#include <iostream>
#include <vector>
#include <array>
#include <stdexcept>
#include <string>

using namespace std;

template<typename T>
class LinkedList
{
public:
    struct Node
    {
        T data_;
        Node* next;
        Node* previous;
    };

    void PrintForward() const;
    void PrintReverse() const;
    unsigned int NodeCount() const;
    void AddHead(const T &data);
    LinkedList();
    LinkedList(const LinkedList<T> &list);
    ~LinkedList();

private:
    Node* head = new Node;
    Node* tail = new Node;
    unsigned int count = 0;
};

template<typename T>
LinkedList<T>::LinkedList()
{

}

template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &list)
{

}

template<typename T>
LinkedList<T>::~LinkedList()
{

}

template<typename T>
void LinkedList<T>::AddHead(const T &data)
{
    Node* newNode = new Node;

    newNode->data_ = data;

    if (count == 0)
    {
        head = newNode;
        tail = newNode;
        head->next = nullptr;
        head->previous = nullptr;
    }
    else
    {
        newNode->next = head;
        head->previous = newNode;
        head = newNode;
    }
    count = count + 1;
}

template<typename T>
void LinkedList<T>::PrintForward() const
{
    Node* currentNode = head;
    while (currentNode != nullptr)
    {
        cout << currentNode->data_ << endl;
        currentNode = currentNode->next;
    }
}

template<typename T>
void LinkedList<T>::PrintReverse() const
{
    Node* currentNode2 = tail;
    while (currentNode2 != nullptr)
    {
        cout << currentNode2->data_ << endl;
        currentNode2 = currentNode2->previous;
    }
}

template<typename T>
unsigned int LinkedList<T>::NodeCount() const
{
    return count;
}

这应该是程序的输出:

测试AddHead() 节点数:6 列印清单: 10 8 6 4 2 0 反向打印列表: 0 2 4 6 8 10

该程序可以正常工作并提供给我正确的输出,但是问题是,当它到达程序底部的“ 10”时,它只是崩溃了,我不知道为什么,有人可以告诉我为什么会这样并且一种可能的解决方法?谢谢

1 个答案:

答案 0 :(得分:1)

您面临的直接问题是,您永远不会将新节点previous的指针设置为nullptr(诚实地说,应该通过更好地构造循环和/或为Node构造适当的错误来纠正此问题。 )。不管了,这里...

template<typename T>
void LinkedList<T>::AddHead(const T &data)
{
    Node* newNode = new Node;

    newNode->data_ = data;

    if (count == 0)
    {
        head = newNode;
        tail = newNode;
        head->next = nullptr;
        head->previous = nullptr;
    }
    else
    {
        newNode->next = head;
        newNode->previous = nullptr; // ADD THIS
        head->previous = newNode;
        head = newNode;

    }
    count = count + 1;
}

在这方面仍然有几处错误:内存泄漏,空的copy-ctor和destructor等,但是以上是当前恶魔的根源。该行也可能是:

newNode->previous = head->previous;

但是坦率地说,这只会混淆您的工作。您总是将新节点放在列表的顶部,因此,相同的previous成员将始终为nullptr(至少在开始研究循环列表之前)。