• C++单链表操作


    #include <stdio.h>

    typedef struct _Node
    {
       int value;
       _Node *next;
    }Node;

    void AddNodeTail(Node *&head, int value)
    {
        Node *newNode = new Node;
        newNode->value = value;
        newNode->next = NULL;
        if(head == NULL)
        {
            head = newNode;
        }
        else
        {
            Node *p = head;
            while(p->next != NULL)
            {
                p = p->next;
            }
            p->next = newNode;
        }
    }

    bool FindNode(const Node *head, int value)
    {
        Node *p = const_cast<Node*>(head);
        bool found = false;
        while(p != NULL)
        {
            if(p->value == value)
            {
                found = true;
                break;
            }
            p = p->next;
        }
        return found;
    }

    void DeleteNode(Node *&head, int value)
    {
        if(head == NULL)
        {
            return;
        }

        Node *delNode = NULL;
        if(head->value == value)
        {
            delNode = head;
            head = head->next;
        }
        else
        {
            Node *pNode = head;
            while(pNode->next != NULL && pNode->next->value != value)
            {
                pNode = pNode->next;
            }
            if(pNode->next != NULL && pNode->next->value == value)
            {
                delNode = pNode->next;
                pNode->next = pNode->next->next;
            }
        }

        if(delNode)
        {
            delete delNode;
        }
    }

    void DeleteAllNode(Node *&head, int value)
    {
        while(head != NULL && head->value == value)
        {
            Node *delNode = head;
            head = head->next;
            delete delNode;
        }
        Node *pNode = head;
        while(pNode->next != NULL)
        {  
            if(pNode->next->value == value)
            {
                Node *delNode = pNode->next;
                pNode->next = pNode->next->next;
                delete delNode;
            }
            else
            {
                pNode = pNode->next;
            }
        }
    }

    void PrintList(Node *head)
    {
        while(head != NULL)
        {
            printf("%d  ", head->value);
            head = head->next;
        }
        printf(" ");
    }

    void ReverseList(Node *&head)
    {
        //空或者仅包含一个节点
        if(head == NULL || head->next == NULL)
        {
            return;
        }
        Node *preNode = head;
        Node *curNode = head->next;
        preNode->next = NULL;
        while(curNode != NULL)
        {
            Node *nextNode = curNode->next;
            curNode->next = preNode;
            preNode = curNode;
            curNode = nextNode;
        }
        head = preNode;
    }

    int main()
    {
        PrintList(NULL);
        Node *head = NULL;
        AddNodeTail(head, 10);
        AddNodeTail(head, 20);
        AddNodeTail(head, 15);
        AddNodeTail(head, 40);
        AddNodeTail(head, 15);
        PrintList(head);
       
        printf("found %d in list %s ", 20, FindNode(head, 20) ? "true" : "false");
        printf("found %d in list %s ", 30, FindNode(head, 30) ? "true" : "false");

        DeleteNode(head, 10);
        PrintList(head);
        DeleteAllNode(head, 15);
        PrintList(head);
        DeleteNode(head, 15);
        PrintList(head);

        Node *nullNode = NULL;
        DeleteNode(nullNode, 0);

        Node *sameHead = NULL;
        AddNodeTail(sameHead, 10);
        AddNodeTail(sameHead, 10);
        AddNodeTail(sameHead, 15);
        AddNodeTail(sameHead, 15);
        AddNodeTail(sameHead, 10);
        AddNodeTail(sameHead, 15);
        AddNodeTail(sameHead, 10);
        PrintList(sameHead);
        DeleteAllNode(sameHead, 10);
        PrintList(sameHead);

        Node *oneNode = NULL;
        AddNodeTail(oneNode, 1);
        ReverseList(oneNode);
        PrintList(oneNode);

        Node *twoNode = NULL;
        AddNodeTail(twoNode, 1);
        AddNodeTail(twoNode, 2);
        ReverseList(twoNode);
        PrintList(twoNode);

        Node *fiveNode = NULL;
        AddNodeTail(fiveNode, 1);
        AddNodeTail(fiveNode, 2);
        AddNodeTail(fiveNode, 3);
        AddNodeTail(fiveNode, 4);
        AddNodeTail(fiveNode, 5);
        ReverseList(fiveNode);
        PrintList(fiveNode);
        return 0;
    }

  • 相关阅读:
    Android开源项目分类汇总
    android开发之Tabhost刷新
    android开发之图表
    通过易宝实现网上支付
    HTTP协议
    汽车美容管理系统质量属性
    框架选择的原因及其说明
    浅析软件架构
    《需求分析与系统设计》读书笔记3
    《需求分析与系统设计》读书笔记2
  • 原文地址:https://www.cnblogs.com/zhouLee/p/6879110.html
Copyright © 2020-2023  润新知