Create a single linked list, search, delete, print order, reverse printing (to prove safety offer)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_39675633/article/details/78665495

Create a single list and look for the elements, remove elements, the order of printing a single list element, reverse (reverse) to print a single list element.


#include <cassert>
#include <iostream>
//默认不使用栈实现反序打印链表中的元素
#define USE_STACK 0
#if USE_STACK
#include <stack>
#endif

struct ListNode
{
    int mVal;
    ListNode* mpNext;
};
/**
*@pHead 使用指针的指针,使得函数结束后pHead指向的节点有效
*@val 待插入的值
*/
void addNodeToTail(ListNode** pHead, int val)
{
    ListNode* pNewNode = new ListNode;
    pNewNode->mVal = val;
    pNewNode->mpNext = nullptr;
    //如果头节点为空
    if (*pHead == nullptr)
    {
        *pHead = pNewNode;
    }
    else
    {
    ListNode* pNode = *pHead;
    while (pNode->mpNext != nullptr)
        pNode = pNode->mpNext;
        pNode->mpNext = pNewNode;

    }
}

void destroyList(ListNode** pHead)
{
   std::cout << "\nstart free memery..." << std::endl;
    ListNode* pNode = *pHead;
    while (*pHead != nullptr)
    {
            *pHead = (*pHead)->mpNext;
        delete pNode;
        pNode = *pHead;
    }
    std::cout << "free memery complete" << std::endl;
}
//从头到尾打印链表
void printList(ListNode**pHead)
{
    ListNode *pNode =*pHead;
    while (pNode != nullptr)
    {
        std::cout << pNode->mVal << " ";
    pNode = pNode->mpNext;
    }
}

//从尾到头打印链表,使用递归实现且不该变原列表的结构
void printReverseList(ListNode* pHead)
{
    if (pHead != nullptr)
    {
        if (pHead->mpNext != nullptr)
    {
        printReverseList(pHead->mpNext);
    }

    std::cout << pHead->mVal << "\t";
    }
}
#if USE_STACK
void printReverseList_stack(ListNode* pHead)
{
    std::stack<ListNode*> nodes;
    ListNode* pNode = pHead;
    while (pNode != nullptr)
    {
        nodes.push(pNode);
    pNode = pNode->mpNext;
    }

    while (!nodes.empty())
    {
        pNode = nodes.top();
    std::cout << pNode->mVal << "\t";
    nodes.pop();
    }
}
#endif

bool findVal(ListNode** pHead, int val)
{
    ListNode* pNode = *pHead;
    while(pNode != nullptr && pNode->mVal != val)
    {
        pNode = pNode->mpNext;
    }

    return pNode == nullptr ? false : true;

}
/*
*作用: 删除指定元素为val的节点
*@pHead 指向头指针
*@val 指定元素为val
*/
void removeNode(ListNode**pHead, int val) 
{
    if (*pHead == nullptr)
        return;
    //将要被删除的节点
    ListNode* pToBeDeleted = nullptr;
    //若删除的节点是头节点
    if ((*pHead)->mVal == val)
    {
        pToBeDeleted = *pHead;
        *pHead = nullptr;
    }
    else
    {
        ListNode* pNode = *pHead;
    //查找要删除的元素,while结束时返回要查找节点的前一个节点
    while (pNode->mpNext != nullptr && pNode->mpNext->mVal != val)
    {
        pNode = pNode->mpNext;
    }
        //找到要删除的节点
    if (pNode->mpNext != nullptr && pNode->mpNext->mVal == val)
    {
        pToBeDeleted = pNode->mpNext;
        //指向要删除节点的下一个节点
        pNode->mpNext = pNode->mpNext->mpNext;
    }
    }

    if (pToBeDeleted != nullptr)
    {
        delete pToBeDeleted;
    pToBeDeleted = nullptr;
    }
}

int main(int argc, char* argv[])
{
    ListNode* pHead = nullptr;
    int val;
    //val == 0为结束标志
    while (std::cin >> val && val != 0)
        addNodeToTail(&pHead,  val);
    std::cout << "打印初始化链表:";
    printList(&pHead);

#if USE_STACK
    printReverseList_stack(pHead);
#endif 
/*
    std::cout << "\n反序打印链表: ";
    printReverseList(pHead);
    std::cout << "\n";
    printList(&pHead);
    std::cout << std::endl;
*/

/*
    std::cout << "\n请输入要查找的元素: ";
    int find_val;
    while (std::cin >> find_val && find_val != 0)
    {
        if (findVal(&pHead, find_val))
        std::cout << "存在该元素" << std::endl;
    else
       std::cout << "不存在该元素" << std::endl;
        std::cout << "请输入要查找的元素: ";
    }

    std::cout << "\n请输入要删除的节点: ";
    int remove_val;
    while (std::cin >> remove_val && remove_val != 0)
    {
        removeNode(&pHead, remove_val);
    std::cout << "删除后的节点序列为 : " << std::endl;
        printList(&pHead);
        std::cout << "\n请输入要删除的节点: ";

    }
    */
    //释放申请的内存空间
    destroyList(&pHead);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_39675633/article/details/78665495