DoubleLinkedList添加元素不起作用,以某种方式始终保持为空

时间:2019-08-08 06:31:16

标签: c++ data-structures

正如我在DoubleLinkedList之前所说的那样,始终保持空白...

我重读了代码。首先,添加实用程序似乎有点可疑,但对我来说似乎正确。我尝试将元素添加到列表中,但是当我调用 empty()函数时,它始终显示 True 。给定的功能对我来说似乎是正确的。

template <typename T>
class DoubleLinkedList; // simple class declaration

template <typename T>
class Node { // double linked list node
private:
    T data; // node element value
    Node *next; // next node in list
    Node *previous; // previous node in list

    friend DoubleLinkedList<T>; // allow DoubleLinkedList access
};

template <typename T>
class DoubleLinkedList { // doubly linked list;
public:
    DoubleLinkedList(); // constructor
    ~DoubleLinkedList(); // destructor

    bool empty() const; // is list empty?
    const T& front() const; // get front element
    const T& back() const; // get back element
    void add_front(const T& e); // add to front of list
    void add_back(const T& e); // ad to back of list
    void remove_front(); // remove from front
    void remove_back(); // remove from back
    void ordered_traverse() const;

private: // local type definitions
    // list sentinels (dummy nodes)
    Node<T>* header;
    Node<T>* tailer;

protected: // local utilities
    void add(Node<T>* v, const T& e); // insert new node before v
    void remove(Node<T>* v); // remove v node
};

template <typename T>
DoubleLinkedList<T>::DoubleLinkedList() { //constructor
    // create sentinels
    header = new Node<T>;
    tailer = new Node<T>;

    // have them point to each other
    header->next = tailer;
    tailer->previous = header;
}

template <typename T>
// destructor, removing all but sentinels; remove the sentinels
DoubleLinkedList<T>::~DoubleLinkedList() { while (!empty()) {remove_front(); delete header; delete tailer; }}

template <typename T>
void DoubleLinkedList<T>::ordered_traverse() const {
    Node<T>* cur = header->next;

    cout << "Starting Traversing... \n";

    while (cur != tailer){
        cout << cur->data << endl;
    }
}

template <typename T>
// is list empty?
bool DoubleLinkedList<T>::empty() const { return header->next == tailer;}

template <typename T>
// get front element
const T& DoubleLinkedList<T>::front() const { return header->next->data; }

template <typename T>
// get back element
const T& DoubleLinkedList<T>::back() const { return tailer->previous->data; }

template <typename T>
// insert new node before v
void DoubleLinkedList<T>::add(Node<T> *v, const T &e) {
    auto* u = new Node<T>; u->data = e; // create a new node for e
    // link new node to its appropriate place
    u->next = v;
    u->previous = v->previous;
    v->previous->next = v->previous = u;
}

template <typename T>
// add to front of list
void DoubleLinkedList<T>::add_front(const T &e) { add(header->next, e);}

template <typename T>
// add to back of list
void DoubleLinkedList<T>::add_back(const T &e) { add(tailer, e);}

template <typename T>
// remove node v
void DoubleLinkedList<T>::remove(Node<T> *v) {

    if (empty())
        throw std::out_of_range("Error: list is empty... \n");


    Node<T>* u = v->previous;
    Node<T>* w = v->next;

    u->next = w;
    w->previous = u;

    delete v;
}

template <typename T>
// remove from front
void DoubleLinkedList<T>::remove_front() { remove(header->next);}

template <typename T>
// remove from back
void DoubleLinkedList<T>::remove_back() { remove(tailer->previous);}

我需要添加方法以应有的方式工作,如果您不介意,请给我一些有关代码的其他缺陷的建议,由于我的无知,这些缺陷我无法看到。

2 个答案:

答案 0 :(得分:0)

ordered_traverse()将始终以永无休止的循环结束。您可能需要:cur = cur->next;在while循环中。那也许可以解释为什么不打印数据?

答案 1 :(得分:0)

此代码段存在问题

void DoubleLinkedList<T>::add(Node<T> *v, const T &e) {
auto* u = new Node<T>; u->data = e; // create a new node for e
// link new node to its appropriate place
u->next = v;
u->previous = v->previous
v->previous->next = v->previous = u; /* this is causing the problem */
}

语句v->previous->next = v->previous = u;引起了问题。赋值运算符从右到左关联。因此,第一个赋值导致v-> previous指向u,整个代码被破坏。  您可以将语句更改为:

v->previous->next = u;
v->previous = u;

这可以解决您的问题。 希望对您有所帮助,谢谢。