zoukankan      html  css  js  c++  java
  • C语言的链表-第一部分

    这是有空头的链表

    #include"stdio.h"
    #include"stdlib.h"
    #include"windows.h"
    
    struct Node
    {
        int a;
        struct Node* pNEXT;
    };
    
    //链表头尾指针定义
    struct Node* g_pHead = NULL;
    struct Node* g_pEnd = NULL;
    
    
    //空头的初始化函数
    void List_Init_Head()
    {
        g_pHead = (struct Node*)malloc(sizeof(struct Node));
        g_pHead->pNEXT = NULL;
        g_pEnd = g_pHead;  //空头的初始化
    }
    
    //创建节点的函数
    struct Node* Create_Node(int N)
    {
        struct Node* pTemp = (struct Node*)malloc(sizeof(struct  Node));
        pTemp->a = N;
        pTemp->pNEXT = NULL;
        return pTemp;
    }
    
    
    //  尾添加
    void AddNodeToList_End(int N)
    {
        //创建一个节点
        struct Node* pTemp = Create_Node(N);
    
        //链接
            g_pEnd->pNEXT = pTemp;
            g_pEnd = pTemp;
    }
    
    
    // 头添加
    void AddNodeToList_Head(int N)
    {
        //创建一个节点
        struct Node* pTemp = Create_Node(N);
                
         pTemp->pNEXT =g_pHead->pNEXT ;
         g_pHead->pNEXT = pTemp;
        
    }
    
    //遍历链表
    void List_SeeAll(struct Node* TouPlace)  //TouPlace 为g_Head
    {
    
        if (TouPlace== NULL)
        {
            printf("链表为空");
            return;
        }
        struct Node* CANSHU = TouPlace->pNEXT;
        printf("当前的链表为
    ");
            while (CANSHU != NULL)
            {
                printf("%d
    ", CANSHU->a);
                CANSHU = CANSHU->pNEXT;
            }
    }
     
    
    //查找数据
    struct Node* List_Search(struct Node* TouPlace, int se)
    {
        struct Node* CANSHU = TouPlace->pNEXT;
        if (CANSHU == NULL)
        {
            exit(0);
            printf("当前链表为空
    ");
        }
        else
        {
            while (CANSHU != NULL)
            {
                if (CANSHU->a == se)
                {
                    return CANSHU;                //返回节点       
                }
                CANSHU = CANSHU->pNEXT;
            }
        }
        return NULL;                            //没找到,返回NULL
    }
    
    
    
    
    
    //指定特定(位置)内容的后一位插入节点
    void Add_List_Behind_Special(struct Node* TouPlace, int index, int N)
    {
        if (TouPlace->pNEXT == NULL)
        {
            printf("此链表为空
    ");
            return;
        }
        else
        {
            struct Node* pTemp = List_Search(TouPlace, index);
            if (pTemp == NULL)
            {
                printf("没有指定节点
    ");
                return;
            }
            else
            {
                struct Node* pNode = Create_Node(N);
                if (pTemp == g_pEnd)
                {
                    AddNodeToList_End(N);
                }
                else
                {
                    pNode->pNEXT = pTemp->pNEXT;
                    pTemp->pNEXT = pNode;
                }
                
            }
        }
    }
    
    //指定特定(位置)内容的前一位插入节点
    void Add_List_Front_Special(struct Node* TouPlace, int index, int N)
    {
        if (TouPlace->pNEXT == NULL)
        {
            printf("此链表为空
    ");
            return;
        }
        else
        {
            struct Node* pTemp = List_Search(TouPlace, index-1);
            if (pTemp == NULL)
            {
                printf("没有指定节点
    ");
                return;
            }
            else
            {
                struct Node* pNode = Create_Node(N);
    
                    pNode->pNEXT = pTemp->pNEXT;
                    pTemp->pNEXT = pNode;
            }
        }
    }
    
    //删除头
    void Delete_List_Head(struct Node* TouPlace) // TouPlace应为g_pHead
    {
        if (NULL == TouPlace->pNEXT)
        {
            printf("当前的链表为空
    ");
            return;
        }
        else
        {
            struct Node* pTemp = g_pHead->pNEXT;
            g_pHead->pNEXT = pTemp->pNEXT;
            free(pTemp);
        }
    }
    
    
    //删除尾
    void Delete_List_End(struct Node* TouPlace, struct Node* WeiPlace)//TouPlace应为g_pHead,WiePlace 是g_pEnd
    {
        if (NULL == TouPlace)
        {
            printf("当前的链表为空
    ");
            return;
        }
        else
        {
    
            if (TouPlace->pNEXT == WeiPlace)//只有一个
            {
                free(g_pEnd);
                g_pHead->pNEXT = NULL;
                g_pEnd=g_pHead;
            }
            else
            {
                //找到倒数第二个
                struct Node* pTemp = g_pHead->pNEXT;
                while (pTemp!=NULL)
                {
                    if (g_pEnd == pTemp->pNEXT)
                    {
                        break;
                    }
                    pTemp = pTemp->pNEXT;
                }
    
                //删除 
                free(g_pEnd);
                g_pEnd = pTemp; 
                g_pEnd->pNEXT = NULL;
            }
        }
    }
    
    
    //删除任意节点
    void Delete_List_Special(struct Node* TouPlace, struct Node* weiPlace, int index)//TouPlace应为g_pHead,WiePlace 是g_pEnd
    {
        if (NULL == (*TouPlace).pNEXT)
        {
            printf("当前的链表为空
    ");
            return;
        }
        else
        {
            struct Node* pTemp = List_Search(g_pHead, index);
            if (pTemp==NULL)
            {
                printf("查无此节点");
                return;
            }
            else
            {
                    if (pTemp == g_pEnd)
                    {
                        Delete_List_End(g_pHead, g_pEnd);
                    }
                    else
                    {
                        struct Node* pT = g_pHead;
                        while (pT != NULL)
                        {
                            if (pTemp == pT->pNEXT)
                            {
                                break;
                            }
                            pT = pT->pNEXT;
                        }
                        pT->pNEXT = pTemp->pNEXT;
                        free(pTemp);
                    }
            }
        }
    }
    
    
    //替换
    void List_Replace_Special(struct Node* TouPlace, int se, int Aim)
    {
        struct Node* UI = List_Search(TouPlace, se);
        if (UI == NULL)
        {
            printf("查找失败");
            exit(0);
        }
        else
        {
            UI->a = Aim;
        }
    }
    
    
    //清空链表
    void List_Free_All(struct Node* TouPlace)// TouPlace应为g_pHead
    {
        struct Node* pTemp = TouPlace->pNEXT;
        while (pTemp != NULL)
        {
            struct Node* pt = pTemp;            //构建中间参数
            pTemp = pTemp->pNEXT;                //先指向下一个,再释放
            free(pt);                            //释放内存
        }
        //清空链表头尾指针
        g_pHead = NULL;
        g_pEnd = NULL;
    }
    
    
    int main()
    {
        List_Init_Head();//空头初始化函数
    
        AddNodeToList_End(1);
        AddNodeToList_End(2);
        AddNodeToList_End(3);
        AddNodeToList_End(4);
        List_SeeAll(g_pHead);
        List_Replace_Special(g_pHead,3,45);
        List_SeeAll(g_pHead);
        return 0;
    }    
  • 相关阅读:
    js、php 判断用户终端 、浏览器类型
    网站安装 https 证书
    PHP请求远程地址设置超时时间
    js实现复制文本内容到剪切板
    微信公众号授权获取用户信息
    生成微信公众号二维码(用户扫码关注公众号)
    域名dns 查询
    服务端 安装配置 svn
    自动生成文档
    python tkinter 布局
  • 原文地址:https://www.cnblogs.com/Loving-Q/p/12629643.html
Copyright © 2011-2022 走看看