努力将双重链接的通用列表转换为循环。 (段错误)

时间:2015-05-11 18:23:30

标签: c++ templates linked-list

所以我得到了这个非循环的双链表样本,我觉得很难将它转换成循环的样本。 (在//添加的行上分段) 任何建议都会很好。 提前致谢。 另外,我应该使用重载来处理不同类型的输入吗?我该如何操纵它们?一个带有正确例子的链接(实际上找不到一个)会让我高兴。

的main.cpp

#include <iostream>
#include "dlring.cpp"
int main()
{
    dlring<int> dlist;
    dlist.Append( 11 );
    dlist.Push( 100 );
    while(dlist)
    std::cout << dlist.pop_back()  << " ";

}

dlring.h

           template <typename T>
        class dlring
        {
            struct node
            {
                T data;
                node* prev;
                node* next;
                node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
            };
            node* head;
            node* tail;
        public:
            dlring() : head(nullptr), tail (nullptr) {}
            bool empty() const { return ( !head || !tail ); }
            operator bool() const { return !empty(); }
//Yes, I also wish to implement this bool stuff otherwise.
            void Append(T);
            void Push(T);
            T pop_back();
            T pop_front();
            ~dlring()
            {
                while(head)
                {
                    node* temp(head);
                    head=head->next;
                    delete temp;
                }
            }
        };

dlring.cpp

#include "dlring.h"
#include <iostream>
template <typename T>
void dlring<T>::Append(T data)
{
    tail = new node(data, tail, head); //changed NULL=>head
    if( tail->prev )
    {
        tail->prev->next = tail;
        head->prev = tail; //added
    }
    if( empty() )
        head = tail;
}
template <typename T>
void dlring<T>::Push(T data)
{
    head = new node(data, tail, head);   //changed NULL=>tail
    if( head->next )
    {
        head->next->prev = head;
        tail->next = head; //added
    }
    if( empty() )
        tail = head;
}
template<typename T>
T dlring<T>::pop_back()
{
    if( empty() )
        std::cout<<"List empty";
    node* temp(tail);
    T data( tail->data );
    tail = tail->prev ;
    if( tail )
    {
        tail->next = head; //null=>head;
        head->prev = tail; //added
    }
    else
        head = nullptr; //NULL=>nullptr
    delete temp;
    return data;
}
template<typename T>
T dlring<T>::pop_front()
{
    if( empty() )
        std::cout<<"List empty";
    node* temp(head);
    T data( head->data );
    head = head->next ;
    if( head )
    {
        head->prev = tail; //NULL=>nullptr=>tail
        tail->next = head;
    }
    else
        tail = nullptr; //NULL=>nullptr
    delete temp;
    return data;
}

1 个答案:

答案 0 :(得分:0)

您的问题出在pop_back函数中:

node * temp(tail);
T data(tail->data);
tail = tail->prev;

if(tail)
{
    tail->next = head; //null=>head;
    head->prev = tail; //added
}
else
{
    head = nullptr; //NULL=>nullptr
}

delete temp;

如果列表中只有一个节点,tail不是null - 这意味着将满足if条件,并且会触及以下代码:

tail->next = head; //null=>head;
head->prev = tail; //added

但是这段代码什么都不做,因为tailhead是一回事。然后,您删除列表中的唯一节点,headtail不再指向任何内容。要解决这个问题,您需要一种更好的方法来检测删除后列表是否为空,并稍微改变删除代码:

if (tail != temp)
{
    tail->next = head; //null=>head;
    head->prev = tail; //added
}
else
{
    head = nullptr; //NULL=>nullptr
    tail = nullptr;
}

delete temp;
temp = nullptr;