kevin55

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#ifndef List_h__
#define List_h__

#include <stdio.h>

struct ListNode
{
    int value;
    ListNode* pNext;
    ListNode(int n) :value(n), pNext(nullptr){}
};

class ListSolution
{
public:
    ListSolution& AddToTail(ListNode** ppHead, int value);
    ListSolution& RemoveNode(ListNode** ppHead, int value);
    ListSolution& RemoveAll(ListNode** ppHead);
    void Print(ListNode* pHead);
};

#endif // List_h__
#include "List.h"

ListSolution& ListSolution::AddToTail(ListNode** ppHead, int value)
{
    if (!ppHead)
    {
        return *this;
    }

    ListNode* pNewNode = new ListNode(value);

    if (nullptr == *ppHead)
    {
        *ppHead = pNewNode;
    }
    else
    {
        ListNode* pCurNode = *ppHead;
        while (pCurNode->pNext)
        {
            pCurNode = pCurNode->pNext;
        }

        pCurNode->pNext = pNewNode;
    }

    return *this;
}

ListSolution& ListSolution::RemoveNode(ListNode** ppHead, int value)
{
    if (nullptr == ppHead
        || nullptr == *ppHead)
    {
        return *this;
    }

    ListNode* pDelNode = nullptr;
    if (value == (*ppHead)->value)
    {
        pDelNode = *ppHead;
        *ppHead = (*ppHead)->pNext;
    }
    else
    {
        ListNode* pCurNode = (*ppHead);
        while (pCurNode->pNext && pCurNode->pNext->value != value)
        {
            pCurNode = pCurNode->pNext;
        }

        if (nullptr != pCurNode->pNext && pCurNode->pNext->value == value)
        {
            pDelNode = pCurNode->pNext;
            pCurNode->pNext = pCurNode->pNext->pNext;
        }
    }
    
    if (pDelNode)
    {
        printf("remove %d\n", pDelNode->value);
        delete pDelNode;
        pDelNode = nullptr;
    }

    return *this;
}

ListSolution& ListSolution::RemoveAll(ListNode** ppHead)
{
    if (nullptr == ppHead
        || nullptr == *ppHead)
    {
        return *this;
    }

    ListNode* pCurNode = *ppHead;
    ListNode* pDelNode = nullptr;
    while (pCurNode->pNext)
    {
        pDelNode = pCurNode->pNext;
        pCurNode->pNext = pCurNode->pNext->pNext;

        printf("remove %d\n", pDelNode->value);
        delete pDelNode;
        pDelNode = nullptr;
    }

    printf("remove %d\n", (*ppHead)->value);
    delete *ppHead;
    *ppHead = nullptr;

    return *this;
}

void ListSolution::Print(ListNode* pHead)
{
    ListNode* pCurNode = pHead;
    while (pCurNode)
    {
        printf(" %d", pCurNode->value);
        pCurNode = pCurNode->pNext;
    }

    printf("\n");
}
#include "List.h"

int main()
{
    ListSolution s;
    ListNode* pHead = nullptr;

    s.AddToTail(&pHead, 1);
    s.AddToTail(&pHead, 2);
    s.AddToTail(&pHead, 3);
    s.AddToTail(&pHead, 4);
    s.AddToTail(&pHead, 5);
    s.AddToTail(&pHead, 6);
    s.Print(pHead);
    s.RemoveNode(&pHead, 3);
    s.RemoveNode(&pHead, 1);
    s.RemoveNode(&pHead, 2);
    s.Print(pHead);
    s.RemoveAll(&pHead);

    s.AddToTail(nullptr, 1);
    s.Print(nullptr);
    s.RemoveNode(nullptr, 1);
    s.RemoveAll(nullptr);

    if (nullptr == pHead)
    {
        printf("pHead == nullptr\n");
    }

    return 0;
}

 

posted on 2016-01-21 22:04  kernel_main  阅读(401)  评论(0编辑  收藏  举报