码迷,mamicode.com
首页 > 编程语言 > 详细

C++双向链表

时间:2019-08-12 00:56:30      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:inf   sem   else   lin   return   div   pre   ast   amp   

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++双向链表

标签:inf   sem   else   lin   return   div   pre   ast   amp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!