RedWoft

To be or not to be, it is a question.
SingleLinkedList
#ifndef SINGLE_LINKED_LIST_H
#define SINGLE_LINKED_LIST_H

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

template
<typename T>
class SingleLinkedList
{
public:

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

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

    
~SingleLinkedList()
    {
        
// @desc: Destroy all nodes.
        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.
    void Append(const T& p_Data)
    {
        
if (Empty())
        {
            m_pTail 
= new ListNode(p_Data);
            m_pHead 
= m_pTail;
            
++m_uiCount;
        }
        
else
        {
            InsertAfter(m_pTail, p_Data);
        }
    }

    
// @desc: Append new node in the front.
    void Prepend(const T& p_Data)
    {
        ListNodePtr pNewNode 
= new ListNode(p_Data);
        pNewNode
->m_pNext = m_pHead;
        m_pHead 
= pNewNode;
        
if (Empty())
        {
            m_pTail 
= m_pHead;
        }
        
++m_uiCount;
    }

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

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

    
// @desc: Remove the tail of linked list.
    void RemoveTail()
    {
        
if (!Empty())
        {
            
if (m_pHead == m_pTail)
            {
                delete m_pTail;
                m_pTail 
= NULL;
                m_pHead 
= m_pTail;
            }
            
else
            {
                ListNodePtr pCurrentNode 
= m_pHead;
                
while (pCurrentNode->m_pNext != m_pTail)
                {
                    pCurrentNode 
= pCurrentNode->m_pNext;
                }
                delete m_pTail;
                m_pTail 
= pCurrentNode;
                m_pTail
->m_pNext = NULL;
            }
            
--m_uiCount;
        }
    }

    
// @desc: Remove the node that the parameter specified.
    void Remove(ListNodePtr p_pPosition)
    {
        
if (p_pPosition == m_pHead)
        {
            RemoveHead();
        }
        
else if (p_pPosition == m_pTail)
        {
            RemoveTail();
        }
        
else
        {
            ListNodePtr pCurrentNode 
= m_pHead;
            
while (pCurrentNode->m_pNext != p_pPosition)
            {
                pCurrentNode 
= pCurrentNode->m_pNext;
            }
            pCurrentNode
->m_pNext = p_pPosition->m_pNext;
            delete p_pPosition;
            
--m_uiCount;
        }
    }

    
// @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:

    ListNodePtr m_pHead;
    ListNodePtr m_pTail;
    unsigned m_uiCount;
};

#endif

 

posted on 2010-07-13 09:32  RedWoft  阅读(285)  评论(0)    收藏  举报