雙向鏈表的介面的介面程式

来源:https://www.cnblogs.com/ljw-boke/p/18153829
-Advertisement-
Play Games

雙向鏈表的介面的介面程式 /******************************************************************* * * file name: 雙向鏈表的介面的介面程式 * author : [email protected] * date : ...


雙向鏈表的介面的介面程式

/*******************************************************************
 *
 *	file name:	雙向鏈表的介面的介面程式
 *	author	 :  [email protected]
 *	date	 :  2024-4-23
 *	function :
 * 	note	 :  None
 *
 *	CopyRight (c)  2024   [email protected]   All Right Reseverd
 *
 * *****************************************************************/

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

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

} DoubleLList_t;

/********************************************************************
 *
 *	name	 :	DoubleLList_Create
 *	function :  創建一個空雙向鏈表,空鏈表應該有一個頭結點,對鏈表進行初始化
 *	argument :	None
 *
 *	retval	 :  返回創建的鏈表的頭地址
 *	author	 :  [email protected]
 *	date	 :  2024-4-23
 * 	note	 :
 *
 * *****************************************************************/

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;
}

/********************************************************************
 *
 *	name	 :	CircLList_NewNode
 *	function :  創建新的結點,並對新結點進行初始化(數據域 + 指針域)
 *	argument :	@data:需插入的數據
 *
 *	retval	 :  返回新創建鏈表節點的地址
 *	author	 :  [email protected]
 *	date	 :  2024-4-23
 * 	note	 :
 *
 * *****************************************************************/

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;
}

/********************************************************************
 *
 *	name	 :	DoubleLList_HeadInsert
 *	function :  向鏈表的頭部進行數據插入
 *	argument :	@head:目標鏈表
 *				@data:需插入的數據
 *	retval	 :  返回新創建鏈表節點的地址
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_HeadInsert(DoubleLList_t *Head, DataType_t data)
{
	// 備份頭節點
	DoubleLList_t *Phead = Head;
	// 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->prev = New;
	Head->next = New;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向鏈表的尾部進行數據插入
 *	argument :	@head:目標鏈表
 *				@data:需插入的數據
 *	retval	 :  返回1成功0失敗
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

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 *Phead = Head;
	// 4遍歷鏈表找到尾部
	while (Phead->next)
	{
		// 把頭的直接後繼作為新的頭結點
		Phead = Phead->next;
	}
	// 5.把新結點插入到鏈表的尾部
	Phead->next = New;
	New->prev = Phead;
	return true;
}

/********************************************************************
 *
 *	name	 :	DoubleLList_DestInsert
 *	function :  向鏈表的指定位置後進行插入
 *	argument :	@head:目標鏈表
 *				@data:需插入的數據
 *				@dest:插入位置
 *	retval	 :  返回1成功0失敗
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_DestInsert(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
	// 1.創建新的結點,並對新結點進行初始化
	DoubleLList_t *New = LList_NewNode(data);

	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}

	// 2.判斷鏈表是否為空,如果為空
	if (NULL == Head->next)
	{
		return false;
	}
	// 備份頭節點地址
	DoubleLList_t *Phead = Head;
	DoubleLList_t *P = NULL;
	// 找目標數據
	while (NULL != Phead->next)
	{
		Phead = Phead->next;
		if (destval == Phead->data)
		{
			P = Phead;
			break;
		}
	}
	// 找不到目標數據
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目標數據在尾部
	if (NULL == Phead->next)
	{
		DoubleLList_TailInsert(Head, data);
		return true;
	}
	// 目標數據在頭部
	if (Head->next == Phead)
	{
		DoubleLList_HeadInsert(Head, data);
		return true;
	}
	// 開始插入
	New->next = Phead->next;
	Phead->next->prev = New;
	New->prev = Phead;
	Phead->next = New;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  頭刪
 *	argument :	@head:目標鏈表
 *				@data:需刪除的數據
 *
 *	retval	 :  返回1成功0失敗
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_HeadDel(DoubleLList_t *Head, DataType_t data)
{
	// 1.判斷鏈表是否為空,如果為空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 備份首節點地址
	DoubleLList_t *Phead = Head->next;
	// 鏈表只有一個節點
	if (NULL == Phead->next)
	{
		free(Phead);
		Head->next = NULL;
		return true;
	}
	// 刪除
	Head->next = Phead->next;
	Phead->next = NULL;
	Head->next->prev = NULL;
	free(Phead);

	return true;
}
/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  尾刪
 *	argument :	@head:目標鏈表
 *				@data:需刪除的數據
 *
 *	retval	 :  返回1成功0失敗
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_TailDel(DoubleLList_t *Head, DataType_t data)
{
	// 1.判斷鏈表是否為空,如果為空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 備份首節點地址
	DoubleLList_t *Phead = Head->next;
	// 鏈表只有一個節點
	if (NULL == Phead->next)
	{
		free(Phead);
		Head->next = NULL;
		return true;
	}
	// 遍歷找到尾結點
	while (NULL == Phead->next)
	{
		Phead = Phead->next;
	}
	Phead->prev->next = NULL;

	Phead->prev = NULL;
	free(Phead);
	return true;
}
/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  中刪
 *	argument :	@head:目標鏈表
 *				@data:需刪除的數據
 *
 *	retval	 :  返回1成功0失敗
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool DoubleLList_Del(DoubleLList_t *Head, DataType_t data)
{
	// 1.判斷鏈表是否為空,如果為空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}
	// 刪除的節點在頭
	if (Head->next->data == data)
	{
		DoubleLList_HeadDel(Head, data);
		return true;
	}
	// 遍歷到尾結點
	DoubleLList_t *P = NULL;
	// 備份頭節點地址
	DoubleLList_t *Phead = Head;
	while (NULL == Phead->next)
	{
		Phead = Phead->next;
		if (data == Phead->data)
		{
			P = Phead;
			break;
		}
	}

	// 找不到目標數據
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目標數據在尾部
	if (NULL == Phead->next)
	{
		DoubleLList_TailDel(Head, data);
		return true;
	}
	Phead->prev->next = Phead->next;
	Phead->next->prev = Phead->prev;
	Phead->prev = NULL;
	Phead->next = NULL;
	free(Phead);
	return true;
}
/********************************************************************
 *
 *	name	 :	LList_Print
 *	function :  遍歷鏈表
 *	argument :	@head:目標鏈表
 *
 *
 *	retval	 :  none
 *	author	 :  [email protected]
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

void DoubleLList_Print(DoubleLList_t *Head)
{
	// 對鏈表的頭文件的地址進行備份
	DoubleLList_t *Phead = Head;

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

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

​```

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

-Advertisement-
Play Games
更多相關文章
  • 手寫 SpringMVC 底層機制 前景提要:實現的是SpringMVC核心機制 對一些細枝末節的代碼做了簡化,比如字元串的處理... 完成哪些機制 機制一: 通過@RequestMapping ,可以標記一個方法,編寫路徑url,瀏覽器就能通過url完成調用 機制二: 進行依賴註入,使之不需要傳統 ...
  • SpringMVC筆記 SpringMVC介紹 基本介紹 SpringMVC 是WEB 層框架, 接管了Web 層組件, 支持MVC 的開發模式/開發架構 SpringMVC 通過註解,讓POJO 成為控制器,不需要繼承類或者實現介面 SpringMVC 採用低耦合的組件設計方式,具有更好擴展和靈活 ...
  • 1. 安裝 & 配置 & 啟動 MySQL現在的版本主要分為: 5.x 版本,現在互聯網企業中的主流版本,包括:頭條、美圖、百度、騰訊等互聯網公司主流的版本。 8.x 版本,新增了一些了視窗函數、持久化配置、隱藏索引等其他功能。 所以,我們課程會以常用大版本中最新的版本為例來講解,即:5.7.31 ...
  • 本系列深入分析編譯器對於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> ​``` 鏈表、節點的創建 /******************************** ...
一周排行
    -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中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...