首页 > 编程语言 > 详细

C++双向链表

时间:2019-08-12 01:18:28      阅读:128      评论:0      收藏:0      [点我收藏+]
template<class T>
class DLLNode
{
public:
    DLLNode()
    {
        next = prev = 0;
    }
    DLLNode(const T& el, DLLNode* n = 0, DLLNode * p = 0) {
        info = el; next = n; prev = p;
    }

    T info;
    DLLNode* next, * prev;

};
template<class T>
class DoublyLinkedList
{
public:
    DoublyLinkedList()
    {
        head = tail = 0;
    }
    
    void Add(const T&);
    T DeleteLast();

    bool isEmpty()
    {
        return head == 0;
    }

private:
    DLLNode<T>* head, * tail;
};

template<class T>
void DoublyLinkedList<T>::Add(const T& el)
{
    if (tail != 0)
    {
        tail = new DLLNode<T>(el, 0, tail);
        tail->prev->next = tail;
    }
    else
    {
        head = tail = new DLLNode<T>(el);
    }
}

template<class T>
T DoublyLinkedList<T>::DeleteLast()
{
    T el = tail->info;
    if (head == tail)
    {
        delete head;
        head = tail = 0;
    }
    else
    {
        tail = tail->prev;
        delete tail->next;
        tail->next = 0;
    }
    return el;
}

 

C++双向链表

原文:https://www.cnblogs.com/ms_senda/p/11337296.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!