单向单链表

// SinglyLinkedList.h

#ifndef _SINGLYLINKEDLIST_H_
#define _SINGLYLINKEDLIST_H_

template <typename T>
struct node
{
    T data;
    node<T> *next;
    node<T>(const T &pData)
    {
        data = pData;
        next = NULL;
    }
    node<T>()
    {
        data = T();
        next = NULL;
    }
};

template <typename T>
class SinglyLinkedList
{
private:
    node<T> *pHead;
    node<T> *pTail;
    size_t length;

public:
    SinglyLinkedList()
    {
        pHead = NULL;
        pTail = NULL;
        length = 0;
    }
    ~SinglyLinkedList()
    {
        clearAllNodes();
    }
    node<T> *getListHead()
    {
        return pHead;
    }
    node<T> *getListTail()
    {
        return pTail;
    }
    node<T> *indexAt(size_t pIndex)
    {
        if (pIndex < 0 || pIndex >= length)
        {
            return NULL;
        }
        node<T> *ptrNodeResult = pHead;
        for (size_t i = 0; i < pIndex; i++)
        {
            ptrNodeResult = ptrNodeResult->next;
        }
        return ptrNodeResult;
    }
    bool appendToHead(const T &pData)
    {
        node<T> *ptrNodeNew = new node<T>(pData);
        if (NULL == ptrNodeNew)
        {
            return false;
        }
        return appendToHead(ptrNodeNew);
    }
    bool appendToHead(node<T> *pNode, bool isNewBuild = false)
    {
        if (NULL == pNode)
        {
            return false;
        }
        node<T> *ptrNodeNew = pNode;
        if (true == isNewBuild)
        {
            ptrNodeNew = new node<T>(pNode->data);
            if (NULL == ptrNodeNew)
            {
                return false;
            }
        }
        else if (isNodeExist(pNode))
        {
            return false;
        }
        ptrNodeNew->next = pHead;
        pHead = ptrNodeNew;
        if (NULL == pTail)
        {
            pTail = pHead;
        }
        length++;
        return true;
    }
    bool appendToTail(const T &pData)
    {
        node<T> *ptrNodeNew = new node<T>(pData);
        if (NULL == ptrNodeNew)
        {
            return false;
        }
        return appendToTail(ptrNodeNew);
    }
    bool appendToTail(node<T> *pNode, bool isNewBuild = false)
    {
        if (NULL == pNode)
        {
            return false;
        }
        node<T> *ptrNodeNew = pNode;
        if (true == isNewBuild)
        {
            ptrNodeNew = new node<T>(pNode->data);
            if (NULL == ptrNodeNew)
            {
                return false;
            }
        }
        else if (isNodeExist(pNode))
        {
            return false;
        }
        if (NULL == pTail)
        {
            pTail = ptrNodeNew;
            pHead = pTail;
        }
        else
        {
            pTail->next = ptrNodeNew;
            pTail = ptrNodeNew;
        }
        length++;
        return true;
    }
    bool insertAt(size_t pIndex, const T &pData)
    {
        if (pIndex < 0 || pIndex > length)
        {
            return false;
        }
        node<T> *ptrNodeNew = new node<T>(pData);
        if (NULL == ptrNodeNew)
        {
            return false;
        }
        if (0 == pIndex)
        {
            ptrNodeNew->next = pHead;
            pHead = ptrNodeNew;
            if (NULL == pTail)
            {
                pTail = pHead;
            }
        }
        else
        {
            node<T> *ptrNodeBeforeIndex = indexAt(pIndex - 1);
            ptrNodeNew->next = ptrNodeBeforeIndex->next;
            ptrNodeBeforeIndex->next = ptrNodeNew;
        }
        length++;
        return true;
    }
    bool deleteAt(size_t pIndex)
    {
        if (pIndex < 0 || pIndex >= length)
        {
            return false;
        }
        if (0 == pIndex)
        {
            node<T> *ptrNodeTemp = pHead;
            pHead = pHead->next;
            if (NULL == pHead)
            {
                pTail = NULL;
            }
            delete ptrNodeTemp;
        }
        else
        {
            node<T> *ptrNodeBeforeIndex = indexAt(pIndex - 1);
            node<T> *ptrNodeTemp = ptrNodeBeforeIndex->next;
            ptrNodeBeforeIndex->next = ptrNodeBeforeIndex->next->next;
            delete ptrNodeTemp;
        }
        length--;
        return true;
    }
    void clearAllNodes()
    {
        while (NULL != pHead)
        {
            node<T> *ptrNodeTemp = pHead;
            pHead = pHead->next;
            delete ptrNodeTemp;
        }
        pHead = NULL;
        pTail = NULL;
        length = 0;
    }
    void reverse()
    {
        node<T> *ptrNodeTemp = pHead;
        pHead = NULL;
        pTail = NULL;
        length = 0;
        while (NULL != ptrNodeTemp)
        {
            node<T> *ptrNodeInsert = ptrNodeTemp;
            ptrNodeTemp = ptrNodeTemp->next;
            appendToHead(ptrNodeInsert);
        }
    }
    bool isNodeExist(const node<T> *pNode)
    {
        bool isFlag = false;
        node<T> *pNodeTemp = pHead;
        while (NULL != pNodeTemp)
        {
            if (pNodeTemp == pNode)
            {
                isFlag = true;
                break;
            }
            pNodeTemp = pNodeTemp->next;
        }
        return isFlag;
    }
};

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值