C語言數據結構:雙向鏈表的增刪操作

来源:https://www.cnblogs.com/beborn00/p/18153841
-Advertisement-
Play Games

/******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...


/********************************************************************************************************

*

*

* 設計雙向鏈表的介面

*

*

*

* Copyright (c)  2023-2024   [email protected]   All right Reserved

* ******************************************************************************************************/


//指的是雙向鏈表中的結點有效數據類型,用戶可以根據需要進行修改
typedef int DataType_t;

//構造雙向鏈表的結點,鏈表中所有結點的數據類型應該是相同的
typedef struct DoubleLinkedList
{
    DataType_t               data; //結點的數據域
    struct DoubleLinkedList *prev; //直接前驅的指針域
    struct DoubleLinkedList *next; //直接後繼的指針域

}DoubleLList_t;

//創建一個空雙向鏈表,空鏈表應該有一個頭結點,對鏈表進行初始化
DoubleLList_t * DoubleLList_Create(void)
{
    //1.創建一個頭結點並對頭結點申請記憶體
    DoubleLList_t *Head = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
    if (NULL == Head)
    {
        perror("Calloc memory for Head is Failed");
        exit(-1);
    }

    //2.對頭結點進行初始化,頭結點是不存儲數據域,指針域指向NULL
    Head->prev = NULL;
    Head->next = NULL;

    //3.把頭結點的地址返回即可
    return Head;
}

//創建新的結點,並對新結點進行初始化(數據域 + 指針域)
DoubleLList_t * DoubleLList_NewNode(DataType_t data)
{
    //1.創建一個新結點並對新結點申請記憶體
    DoubleLList_t *New = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
    if (NULL == New)
    {
        perror("Calloc memory for NewNode is Failed");
        return NULL;
    }

    //2.對新結點的數據域和指針域(2個)進行初始化
    New->data = data;
    New->prev = NULL;
    New->next = NULL;

    return New;
}

//頭插
bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
    // 1.創建新的結點,並對新結點進行初始化
    DoubleLList_t *New = DoubleLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.如果雙向鏈表為非空,把新結點插入到鏈表的頭部\
    New->next = Head->next;
    Head->next->prev = New;
    Head->next = New;

    return true;
}

//尾插
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
    // 1.創建新的結點,並對新結點進行初始化
    DoubleLList_t *New = DoubleLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.如果雙向鏈表為非空,遍歷鏈表,找到尾結點
    DoubleLList_t *Tail = Head;
    while(Tail->next != NULL)
    {
        Tail = Tail->next;
    }

    // 4.把新結點插入到鏈表的尾部
    Tail->next = New;
    New->prev = Tail;

    return true;
}

//指定插
bool DoubleLList_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{
    // 1.創建新的結點,並對新結點進行初始化
    DoubleLList_t *New = DoubleLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判斷雙向鏈表是否為空,如果為空,則直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.如果雙向鏈表為非空,遍歷鏈表,找到插入位置
    DoubleLList_t *Dest = Head->next;
    while (Dest->data != destval && Dest != NULL)
    {
        Dest = Dest->next;
    }
    if (NULL == Dest)
    {
        return false;
    }

    // 4.說明找到目標結點,則把新結點插入到目標結點的後面
    New->next = Dest->next;
    Dest->next->prev = New;
    New->prev = Dest;
    Dest->next = New;

    return true;
}

//頭刪
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
    // 1.判斷鏈表是否為空,如果為空,則直接退出
    if (NULL == Head->next)
    {
        return false;
    }

    // 2.對鏈表的首結點的地址進行備份
    DoubleLList_t *Temp = Head->next;

    // 3.鏈表是非空的,則直接刪除首結點
    Head->next = Temp->next;
    Temp->next = NULL;
    Temp->next->prev = NULL;
    free(Temp);

    return true;
}

// 尾刪
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
    // 1.判斷鏈表是否為空,如果為空,則直接退出
    if (NULL == Head->next)
    {
        return false;
    }

    // 2.遍歷鏈表,找到尾結點
    DoubleLList_t *Tail = Head;
    while (Tail->next != NULL)
    {
        Tail = Tail->next;
    }

    // 3.刪除尾結點
    Tail->prev->next = NULL;
    Tail->prev = NULL;
    free(Tail);

    return true;
}

// 指定刪
bool DoubleLList_DestDel(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
    // 1.判斷鏈表是否為空,如果為空,則直接退出
    if (NULL == Head->next)
    {
        return false;
    }

    // 2.鏈表是非空的,遍歷鏈表,找到待刪除結點
    DoubleLList_t *Dest = Head;
    while (Dest->data != destval && Dest != NULL)
    {
        Dest = Dest->next;
    }
    if (NULL == Dest)
    {
        return false;
    }

    // 3.刪除指定結點
    Dest->prev->next = Dest->next;
    Dest->next->prev = Dest->prev;
    Dest->prev = NULL;
    Dest->next = NULL;
    free(Dest);

    return true;
}

//遍歷鏈表
bool DoubleLList_Print(DoubleLList_t *Head)
{
    //對雙向鏈表的頭結點的地址進行備份
    DoubleLList_t *Phead = Head;
    //判斷當前鏈表是否為空,為空則直接退出
    if (Head->next == Head)
    {
        printf("current linkeflist is empty!\n");
        return false;
    }

    //從首結點開始遍歷
    while(Phead->next)
    {
        //把頭結點的直接後繼作為新的頭結點
        Phead = Phead->next;

        //輸出頭結點的直接後繼的數據域
        printf("data = %d\n",Phead->data);

        //判斷是否到達尾結點,尾結點的next指針是指向首結點的地址
        if (Phead->next == Head->next)
        {
            break;
        }
    }

    return true;
}

int main(int argc, char const *argv[])
{
    return 0;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本系列深入分析編譯器對於C++虛函數的底層實現,最後分析C++在多態的情況下的性能是否有受影響,多態究竟有多大的性能損失。 ...
  • 作者:張富春(ahfuzhang),轉載時請註明作者和引用鏈接,謝謝! cnblogs博客 zhihu Github 公眾號:一本正經的瞎扯 代碼請看:https://github.com/ahfuzhang/cowmap 有這樣一種場景:數據量不多的map,在使用中讀極多寫極少。為了在這種場景下做 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • /******************************************************************************************************** * * * 設計單向迴圈鏈表的介面 * * * * Copyright (c) 2023 ...
  • 單向迴圈鏈表的介面程式 單向迴圈鏈表 頭文件 #include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h> ​``` 鏈表、節點的創建 /******************************** ...
  • 雙向鏈表的介面的介面程式 /******************************************************************* * * file name: 雙向鏈表的介面的介面程式 * author : [email protected] * date : ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
一周排行
    -Advertisement-
    Play Games
  • 概述:本文代碼示例演示瞭如何在WPF中使用LiveCharts庫創建動態條形圖。通過創建數據模型、ViewModel和在XAML中使用`CartesianChart`控制項,你可以輕鬆實現圖表的數據綁定和動態更新。我將通過清晰的步驟指南包括詳細的中文註釋,幫助你快速理解並應用這一功能。 先上效果: 在 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • 概述:本示例演示了在WPF應用程式中實現多語言支持的詳細步驟。通過資源字典和數據綁定,以及使用語言管理器類,應用程式能夠在運行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的代碼結構。 在WPF中實現多語言的一種常見方法是使用資源字典和數據綁定。以下是一個詳細的步驟和示例源代 ...
  • 描述(做一個簡單的記錄): 事件(event)的本質是一個委托;(聲明一個事件: public event TestDelegate eventTest;) 委托(delegate)可以理解為一個符合某種簽名的方法類型;比如:TestDelegate委托的返回數據類型為string,參數為 int和 ...
  • 1、AOT適合場景 Aot適合工具類型的項目使用,優點禁止反編 ,第一次啟動快,業務型項目或者反射多的項目不適合用AOT AOT更新記錄: 實實在在經過實踐的AOT ORM 5.1.4.117 +支持AOT 5.1.4.123 +支持CodeFirst和非同步方法 5.1.4.129-preview1 ...
  • 總說周知,UWP 是運行在沙盒裡面的,所有許可權都有嚴格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應用模型,本身是沒有什麼許可權管理的,許可權管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那麼有沒... ...
  • 目錄條款17:讓介面容易被正確使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly)限制類型和值規定能做和不能做的事提供行為一致的介面條款19:設計class猶如設計type(Treat class de ...
  • title: 從零開始:Django項目的創建與配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 後端開發 tags: Django WebDev Python ORM Security Deployment Op ...
  • 1、BOM對象 BOM:Broswer object model,即瀏覽器提供我們開發者在javascript用於操作瀏覽器的對象。 1.1、window對象 視窗方法 // BOM Browser object model 瀏覽器對象模型 // js中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...