欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作

 更新時間:2023年04月12日 10:38:40   作者:朦朧的雨夢  
這篇文章主要介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作,關(guān)于數(shù)據(jù)結(jié)構(gòu)鏈表的操作一般涉及的就是增刪改查,下面將關(guān)于無空頭鏈表展開介紹,需要的朋友可以參考下

關(guān)于節(jié)點數(shù)據(jù)添加:

尾添加

最核心的是定義一個頭指針和一個尾指針(尾指針可以不定義但是會增加代碼的重復(fù)性,增加程序運行時間);

關(guān)于尾添加:(注意區(qū)分有節(jié)點和無節(jié)點的情況)

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    endadd(&phead,&pend,4);
    ......
 
 
    return 0;
}
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
  
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *phead = pt;
 
    }
    else
 
    {
        (*pend)->pnext = pt;
    }
    *pend= pt;
}

頭添加

關(guān)于代碼思路與尾添加基本一致,注意區(qū)分節(jié)點的鏈接:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    head_add(&phead,&pend,4);
    ......
 
 
    return 0;
}
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
    
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *pend = pt;
 
    }
    else
 
    {
        pt->pnext = (*phead);
    }
    *phead= pt;
}

一次性添加n個x數(shù)據(jù)節(jié)點:

利用循壞,直接調(diào)用頭添加或者尾添加:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    circulate_add(&phead,&pend,4,5);
    ......
 
 
    return 0;
}
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata);
 
{
    for(int i = 0;i<count;i++)
    {
        endadd(phead, pend, adddata);
       
    }
 
 
 
}

關(guān)于查找:

根據(jù)指定數(shù)據(jù):

核心就是通過頭指針一個一個往下走找到指定節(jié)點的數(shù)據(jù)與所找數(shù)據(jù)是否匹配,最重要的是要使用中間變量記錄頭指針,否則就無法找到頭指針了(因為是單項鏈表):

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void data_find(struct Mystruct *phead, int designated_data);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
void data_find(struct Mystruct* phead, int designated_data)
{
    if (NULL == phead)
        return;
    struct Mystruct* ptemp = phead;
    while (ptemp != NULL)
    {
        if (ptemp->data == designated_data)
        {
            printf("找到了");
            break;
 
        }
        ptemp = ptemp->pnext;
    }
    return;
 
}

根據(jù)下標查找:

思路基本不變;區(qū)別傳入指定下標;內(nèi)部定義一個計數(shù)器,當下標和計數(shù)器數(shù)值相等;表示鏈表存在這個節(jié)點;可以選擇傳出或者提醒;大家思考一下,動手實踐一下。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
struct Mystruct *index_find(struct Mystruct *phead, int index);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
struct Mystruct* index_find(struct Mystruct* phead, int index)
 
{
    if (NULL == phead||index<0)
        return NULL;
 
    struct Mystruct* ptemp = phead;
    int i = 0;
    for (i = 0; i < index; i++)
    {
        ptemp = ptemp->pnext;
    }
    return ptemp;
 
}

刪除頭節(jié)點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_head(struct Mystruct **phead,struct Mystruct **pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_head(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if ((*phead)->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        *phead = (*phead)->pnext;
        free(pt);
    }
 
 
 
 
}
void deleat_end(struct My

刪除尾節(jié)點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_end(struct Mystruct**phead,struct Mystruct**pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_end(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        while (pt->pnext != (*pend))
        {
            if (pt->pnext == (*pend))
            {
                free(*pend);
                *pend = pt;
                pt->pnext = NULL;
                pt = pt->pnext;
            }
           
 
        }
 
    }
    
  
 
 
}

刪除中間節(jié)點:

這里思路改變一下:根據(jù)數(shù)據(jù)或者下標找到前一個節(jié)點,改變前一個節(jié)點的pnext指針的指向,直接指向下一個節(jié)點,也就是這個節(jié)點的pnext;簡單示范一下刪除中間指定數(shù)據(jù):

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
}

刪除全部節(jié)點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_all(&phead,&pend)
    ......
 
 
    return 0;
}
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
{
 
 
    while (*phead!= NULL)
    {
        struct Mystruct* pt = *phead;
        *phead = (*phead)->pnext;
        free(pt);
 
 
    }
    *phead = NULL;
    *pend = NULL;
 
 
 
}

到此這篇關(guān)于關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作的文章就介紹到這了,更多相關(guān)數(shù)據(jù)結(jié)構(gòu)單向鏈表操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Matlab實現(xiàn)生成箭頭坐標軸詳解

    Matlab實現(xiàn)生成箭頭坐標軸詳解

    這篇文章主要介紹了如何利用Matlab實現(xiàn)生成箭頭坐標軸,為坐標軸增添箭頭,文中的示例代碼講解詳細,對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下
    2022-03-03
  • C++之默認參數(shù)詳解

    C++之默認參數(shù)詳解

    這篇文章主要介紹了C++的默認參數(shù),是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-11-11
  • C語言深入探究自定義類型之結(jié)構(gòu)體與枚舉及聯(lián)合

    C語言深入探究自定義類型之結(jié)構(gòu)體與枚舉及聯(lián)合

    今天我們來學(xué)習(xí)一下自定義類型,自定義類型包括結(jié)構(gòu)體、枚舉、聯(lián)合體,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考
    2022-05-05
  • ?C++?new?和?delete?關(guān)鍵字詳解

    ?C++?new?和?delete?關(guān)鍵字詳解

    這篇文章主要介紹了?C++?new?和?delete?關(guān)鍵字詳解,文章圍繞主題展開new?和?delete?的使用方法的介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • C/C++ Zlib庫封裝MyZip壓縮類的詳細過程

    C/C++ Zlib庫封裝MyZip壓縮類的詳細過程

    在軟件開發(fā)中,文件的壓縮和解壓縮是一項常見的任務(wù),而ZIP是一種被廣泛應(yīng)用的壓縮格式,本文將聚焦于一個簡化的C++實現(xiàn),通過分析代碼,我們將深入了解其設(shè)計和實現(xiàn)細節(jié),感興趣的朋友一起看看吧
    2023-11-11
  • c++ 實現(xiàn)文件逐行讀取與字符匹配

    c++ 實現(xiàn)文件逐行讀取與字符匹配

    這里嘗試通過C++來實現(xiàn)一個文件IO的功能,看看是否能夠比python的表現(xiàn)更好一些,感興趣的朋友可以參考下
    2021-05-05
  • C語言中如何利用循環(huán)嵌套輸出一個菱形

    C語言中如何利用循環(huán)嵌套輸出一個菱形

    這篇文章主要介紹了C語言中如何利用循環(huán)嵌套輸出一個菱形問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 一篇文章帶你了解C語言指針進階

    一篇文章帶你了解C語言指針進階

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關(guān)概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下
    2021-09-09
  • c語言基于stdarg.h的可變參數(shù)函數(shù)的用法

    c語言基于stdarg.h的可變參數(shù)函數(shù)的用法

    本篇文章主要介紹了c語言基于stdarg.h的可變參數(shù)函數(shù)的用法,詳細的介紹了可變參數(shù)函數(shù)的詳細用法和源碼實例,有興趣的可以了解一下
    2017-07-07
  • C++ vector在多線程操作中出現(xiàn)內(nèi)存錯誤問題及解決

    C++ vector在多線程操作中出現(xiàn)內(nèi)存錯誤問題及解決

    這篇文章主要介紹了C++ vector在多線程操作中出現(xiàn)內(nèi)存錯誤問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論