RedWoft

To be or not to be, it is a question.
DoubleLinkedList

 

#ifndef DOUBLE_LINKED_LIST_H
#define DOUBLE_LINKED_LIST_H

template
<typename T>
struct DoubleListNode
{
    DoubleListNode(
const T& p_Data):m_Data(p_Data),m_pPrevious(NULL),m_pNext(NULL){}
    T m_Data;
    DoubleListNode
<T>* m_pPrevious;
    DoubleListNode
<T>* m_pNext;
};

template
<typename T>
class DoubleLinkedList
{
public:

    typedef DoubleListNode
<T> ListNode;
    typedef ListNode
* ListNodePtr;

    
// @desc: None.
    DoubleLinkedList():m_pHead(NULL),m_pTail(NULL),m_uiCount(0U){}

    
// @desc: Destroy all nodes.
    ~DoubleLinkedList();

    
// @desc: Append new node in the back.
    void Append(const T& p_Data);

    
// @desc: Append new node in the front.
    void Prepend(const T& p_Data);

    
// @desc: Insert new node behind the position that the parameter specified.
    void InsertAfter(ListNodePtr p_pPosition, const T& p_Data);

    
// @desc: Insert new node before the position that the parameter specified.
    void InsertBefore(ListNodePtr p_pPosition, const T& p_Data);

    
// @desc: Remove the head of linked list.
    void RemoveHead();

    
// @desc: Remove the tail of linked list.
    void RemoveTail();

    
// @desc: Remove the node that the parameter specified.
    void Remove(ListNodePtr p_pPosition);

    
// @desc: Return the number of node.
    unsigned Count() const
    {
        
return m_uiCount;
    }

    
// @desc: Return the symbol of whether linked list is empty.
    bool Empty() const
    {
        
return (Count() == 0U);
    }

private:

    
// @desc: Insert first node.
    void InsertFirstNode(const T& p_Data);

    ListNodePtr m_pHead;
    ListNodePtr m_pTail;
    unsigned m_uiCount;
};

// @desc: Destroy all nodes.
template<typename T>
DoubleLinkedList
<T>::~DoubleLinkedList()
{
    ListNodePtr pNextNode 
= NULL;
    
for (ListNodePtr pCurrentNode = m_pHead; pCurrentNode != NULL; pCurrentNode = pNextNode)
    {
        pNextNode 
= pCurrentNode->m_pNext;
        delete pCurrentNode;
    }
}

// @desc: Append new node in the back.
template<typename T>
void DoubleLinkedList<T>::Append(const T& p_Data)
{
    
if (Empty())
    {
        InsertFirstNode();
    }
    
else
    {
        InsertAfter(m_pTail, p_Data);
    }
}

// @desc: Append new node in the front.
template<typename T>
void DoubleLinkedList<T>::Prepend(const T& p_Data)
{
    
if (Empty())
    {
        InsertFirstNode();
    }
    
else
    {
        InsertBefore(m_pHead, p_Data);
    }
}

// @desc: Insert new node behind the position that the parameter specified.
template<typename T>
void DoubleLinkedList<T>::InsertAfter(ListNodePtr p_pPosition, const T& p_Data)
{
    ListNodePtr pNewNode 
= new ListNode(p_Data);
    pNewNode
->m_pNext = p_pPosition->m_pNext;
    pNewNode
->m_pPrevious = p_pPosition;
    p_pPosition
->m_pNext->m_pPrevious = pNewNode;
    p_pPosition
->m_pNext = pNewNode;
    
if (p_pPosition == m_pTail)
    {
        m_pTail 
= pNewNode;
    }
    
++m_uiCount;
}

// @desc: Insert new node before the position that the parameter specified.
template<typename T>
void DoubleLinkedList<T>::InsertBefore(ListNodePtr p_pPosition, const T& p_Data)
{
    ListNodePtr pNewNode 
= new ListNode(p_Data);
    pNewNode
->m_pNext = p_pPosition;
    pNewNode
->m_pPrevious = p_pPosition->m_pPrevious;
    p_pPosition
->m_pPrevious->m_pNext = pNewNode;
    p_pPosition
->m_pPrevious = pNewNode;
    
if (p_pPosition == m_pHead)
    {
        m_pHead 
= pNewNode;
    }
    
++m_uiCount;
}

// @desc: Remove the head of linked list.
template<typename T>
void DoubleLinkedList<T>::RemoveHead()
{
    
if (!Empty())
    {
        ListNodePtr pNextNode 
= m_pHead->m_pNext;
        delete m_pHead;
        m_pHead 
= pNextNode;
        
if (m_pHead == NULL)
        {
            m_pTail 
= NULL;
        }
        
else
        {
            m_pHead
->m_pPrevious = NULL;
        }
        
--m_uiCount;
    }
}

// @desc: Remove the tail of linked list.
template<typename T>
void DoubleLinkedList<T>::RemoveTail()
{
    
if (!Empty())
    {
        ListNodePtr pPreviousNode 
= m_pTail->m_pPrevious;
        delete m_pTail;
        m_pTail 
= pPreviousNode;
        
if (m_pTail == NULL)
        {
            m_pHead 
= NULL;
        }
        
else
        {
            m_pTail
->m_pNext = NULL;
        }
        
--m_uiCount;
    }
}

// @desc: Remove the node that the parameter specified.
template<typename T>
void DoubleLinkedList<T>::Remove(ListNodePtr p_pPosition)
{
    
if (p_pPosition == m_pHead)
    {
        RemoveHead();
    }
    
else if (p_pPosition == m_pTail)
    {
        RemoveTail();
    }
    
else
    {
        p_pPosition
->m_pPrevious->m_pNext = p_pPosition->m_pNext;
        p_pPosition
->m_pNext->m_pPrevious = p_pPosition->m_pPrevious;
        delete p_pPosition;
        
--m_uiCount;
    }
}

// @desc: Insert first node.
template<typename T>
void DoubleLinkedList<T>::InsertFirstNode(const T& p_Data)
{
    m_pTail 
= new ListNode(p_Data);
    m_pHead 
= m_pTail;
    
++m_uiCount;
}

#endif

 

 

posted on 2010-06-29 16:36  RedWoft  阅读(407)  评论(0)    收藏  举报