C語言鏈表使用

目錄

  • 雙鏈表增刪改查
  • 鏈表帶功能函數

雙鏈表增刪改查

#include <stdio.h>
#include <stdlib.h>// 雙鏈表結點的定義
typedef struct DNode{int data;struct DNode *prev;struct DNode *next;
} DNode;// 創建雙鏈表
DNode *createDoublyLinkedList() {int n, i;printf("請輸入雙鏈表的長度:");scanf("%d", &n);if (n <= 0) {printf("輸入的長度無效!\n");return NULL;}DNode *head = NULL;  // 雙鏈表的頭結點DNode *tail = NULL;  // 雙鏈表的尾結點// 創建雙鏈表的結點for (i = 0; i < n; i++) {int data;printf("請輸入第 %d 個結點的數據:", i+1);scanf("%d", &data);DNode *newNode = (DNode*)malloc(sizeof(DNode));newNode->data = data;newNode->prev = NULL;newNode->next = NULL;if (head == NULL) {head = newNode;tail = newNode;} else {tail->next = newNode;newNode->prev = tail;tail = newNode;}}return head;
}// 遍歷雙鏈表
void traverseDoublyLinkedList(DNode *head) {if (head == NULL) {printf("雙鏈表為空!\n");return;}DNode *current = head;printf("雙鏈表的數據為:");while (current != NULL) {printf("%d ", current->data);current = current->next;}printf("\n");
}// 在指定位置插入結點
void insertNode(DNode **head, int position, int data) {if (*head == NULL) {printf("雙鏈表為空!\n");return;}DNode *current = *head;int currentPosition = 1;// 找到指定位置的結點while (currentPosition < position && current->next != NULL) {current = current->next;currentPosition++;}if (currentPosition < position) {printf("插入位置無效!\n");return;}// 創建新結點DNode *newNode = (DNode*)malloc(sizeof(DNode));newNode->data = data;newNode->prev = NULL;newNode->next = NULL;if (currentPosition == 1) {newNode->next = *head;(*head)->prev = newNode;*head = newNode;} else {newNode->next = current;newNode->prev = current->prev;current->prev->next = newNode;current->prev = newNode;}printf("成功插入結點!\n");
}// 刪除指定位置的結點
void deleteNode(DNode **head, int position) {if (*head == NULL) {printf("雙鏈表為空!\n");return;}DNode *current = *head;int currentPosition = 1;// 找到指定位置的結點while (currentPosition < position && current != NULL) {current = current->next;currentPosition++;}if (current == NULL) {printf("刪除位置無效!\n");return;}if (currentPosition == 1) {*head = (*head)->next;if (*head != NULL) {(*head)->prev = NULL;}} else {current->prev->next = current->next;if (current->next != NULL) {current->next->prev = current->prev;}}free(current);printf("成功刪除結點!\n");
}// 按值查找結點
DNode *searchNode(DNode *head, int data) {if (head == NULL) {printf("雙鏈表為空!\n");return NULL;}DNode *current = head;// 遍歷雙鏈表,找到指定值的結點while (current != NULL) {if (current->data == data) {return current;}current = current->next;}printf("未找到指定結點!\n");return NULL;
}//獲取指定位置數據
int get_node_value(DNode* head, int pos) {if (head == NULL) {  // 判斷鏈表是否為空printf("鏈表為空,無法查找!\n");return -1;}int count = 0;DNode* current = head;while (current != NULL) {if (count == pos) {return current->data;  // 找到指定位置的節點,返回節點的值}count++;current = current->next;  // 繼續遍歷下一個節點}printf("指定位置不存在節點!\n");  // 遍歷完整個鏈表仍然沒有找到節點return -1;
}//查看節點數量
int get_node_count(DNode* head) {int count = 0;DNode* current = head;while (current != NULL) {count++;current = current->next;}return count;
}//打印鏈表數據
void print_list(DNode* head) {DNode* current = head;while (current != NULL) {printf("%d ", current->data);current = current->next;}printf("\n");
}//從尾部添加數據
DNode* create_node(int data) {DNode* newNode = (DNode*)malloc(sizeof(DNode));newNode->data = data;newNode->next = NULL;return newNode;
}
void append_node(DNode** head, int data) {DNode* newNode = create_node(data);if (*head == NULL) {// 如果鏈表為空,則將新節點作為頭節點*head = newNode;} else {// 找到鏈表的末尾節點DNode* current = *head;while (current->next != NULL) {current = current->next;}// 將新節點添加到末尾current->next = newNode;}
}// 釋放雙鏈表的內存
void freeDoublyLinkedList(DNode **head) {DNode *current = *head;while (current != NULL) {DNode *temp = current;current = current->next;free(temp);}*head = NULL;printf("成功釋放雙鏈表的內存!\n");
}int main() {DNode *head = NULL;// 創建雙鏈表head = createDoublyLinkedList();// 遍歷雙鏈表traverseDoublyLinkedList(head);// 在指定位置插入結點int insertPosition, insertData;printf("請輸入要插入的位置和數據(以空格分隔):");scanf("%d %d", &insertPosition, &insertData);insertNode(&head, insertPosition, insertData);traverseDoublyLinkedList(head);// 刪除指定位置的結點int deletePosition;printf("請輸入要刪除的位置:");scanf("%d", &deletePosition);deleteNode(&head, deletePosition);traverseDoublyLinkedList(head);// 按值查找結點int searchData;printf("請輸入要查找的值:");scanf("%d", &searchData);DNode *searchResult = searchNode(head, searchData);if (searchResult != NULL) {printf("成功找到結點!");}int position;printf("請輸入要查找的位置:");scanf("%d", &position);int value = get_node_value(head, position);if (value == -1) {printf("位置超出鏈表范圍!\n");} else {printf("位置 %d 的值為:%d\n", position, value);}   int count = get_node_count(head);printf("鏈表中共有 %d 個數據節點\n", count);append_node(&head, 30);print_list(head);// 釋放雙鏈表的內存freeDoublyLinkedList(&head);return 0;
}

鏈表帶功能函數

#include <stdio.h>
#include <stdlib.h>typedef struct DNode {int data;void (*Function)(const char*);struct DNode* prev;struct DNode* next;
} DNode;// 創建節點
DNode* createNode(int data, void (*function)(const char*)) {DNode* node = (DNode*)malloc(sizeof(DNode));node->data = data;node->Function = function;node->prev = NULL;node->next = NULL;return node;
}// 插入節點到鏈表末尾
void insertNode(DNode** head, DNode* node) {if (*head == NULL) {*head = node;} else {DNode* current = *head;while (current->next != NULL) {current = current->next;}current->next = node;node->prev = current;}
}// 執行鏈表中的節點功能函數
void executeListFunction(DNode* head, const char* param) {DNode* current = head;while (current != NULL) {if (current->Function != NULL) {current->Function(param);}current = current->next;}
}void executeListFunctions(DNode* head, int searchData, const char* param) {DNode* current = head;while (current != NULL) {if (current->data == searchData && current->Function != NULL) {current->Function(param); //執行對應功能函數。}current = current->next;}
}
// 示例的功能函數
void function1(const char* param) {printf("執行功能函數1,參數:%s\n", param);
}void function2(const char* param) {printf("執行功能函數2,參數:%s\n", param);
}int main() {// 創建節點DNode* node1 = createNode(1, function1);DNode* node2 = createNode(2, function2);// 構建鏈表DNode* head = NULL;insertNode(&head, node1);insertNode(&head, node2);// 執行鏈表中的功能函數executeListFunctions(head, 1, "參數");// 釋放內存free(node1);free(node2);return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/167534.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/167534.shtml
英文地址,請注明出處:http://en.pswp.cn/news/167534.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C語言】qsort的秘密

一&#xff0c;本文目標 qsort函數可以對任意類型數據甚至是結構體內部的數據按照你想要的規則排序&#xff0c;它的功能很強大&#xff0c;可是為什么呢&#xff1f; 我將通過模擬實現qsort函數來讓你對這整個過程有一個清晰的深刻的理解。 二&#xff0c;qsort函數原型 v…

leetcode刷題詳解一

算法題常用API std::accumulate 函數原型&#xff1a; template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init );一般求和的&#xff0c;代碼如下&#xff1a; int sum accumulate(vec.begin() , vec.end() , 0);詳細用法參考 lo…

【python海洋專題四十七】風速的風羽圖

【python海洋專題四十七】風速的風羽圖 圖片 往期推薦 圖片 【python海洋專題一】查看數據nc文件的屬性并輸出屬性到txt文件 【python海洋專題二】讀取水深nc文件并水深地形圖 【python海洋專題三】圖像修飾之畫布和坐標軸 【Python海洋專題四】之水深地圖圖像修飾 【Pyth…

記一次linux操作系統實驗

前言 最近完成了一個需要修改和編譯linux內核源碼的操作系統實驗&#xff0c;個人感覺這個實驗還是比較有意思的。這次實驗總共耗時4天&#xff0c;從對linux實現零基礎&#xff0c;通過查閱資料和不斷嘗試&#xff0c;直到完成實驗目標&#xff0c;在這過程中確實也收獲頗豐&…

pytorch模型優化簡介,未完結版

如有幫助&#xff0c;點贊收藏關注&#xff01; 如需轉載&#xff0c;請注明出處&#xff01; 今天來介紹torch模型中的優化器 優化是指在每個訓練步驟中調整模型參數以減少模型誤差的過程。 優化算法定義如何執行這個過程 所有優化邏輯都封裝在優化器對象中。在這里&#xf…

【黑馬甄選離線數倉day04_維度域開發】

1. 維度主題表數據導出 1.1 PostgreSQL介紹 PostgreSQL 是一個功能強大的開源對象關系數據庫系統&#xff0c;它使用和擴展了 SQL 語言&#xff0c;并結合了許多安全存儲和擴展最復雜數據工作負載的功能。 官方網址&#xff1a;PostgreSQL: The worlds most advanced open s…

音視頻項目——RTSP服務器解析(1)

介紹 利用線程池&#xff0c;實現 RTSP 服務器的高并發請求處理。 RTSP 是音視頻的控制視頻的協議&#xff0c;如果您還不了解&#xff0c;可以看看之前我解析 RTSP 協議的文章。音視頻協議解析(RTP/RTCP/RTSP/RTMP)——RTSP解析-CSDN博客 解析 我們先來看 RTP 的實現。RTP 是音…

Django框架之auth模塊

目錄 一、Auth模塊引入 二、創建超級用戶(管理員) 三、依賴于auth_user表完成登錄注冊功能 【1】基礎登陸 【2】保存用戶狀態 【3】登錄后跳轉 (1) 登錄后才能訪問頁面 -- 局部配置 (2) 登錄后才能訪問頁面 -- 全局配置 (3) 小結 三、修改密碼 四、注銷 五、注冊…

Springboot將多個圖片導出成zip壓縮包

Springboot將多個圖片導出成zip壓縮包 將多個圖片導出成zip壓縮包 /*** 判斷時間差是否超過6小時* param startTime 開始時間* param endTime 結束時間* return*/public static boolean isWithin6Hours(String startTime, String endTime) {// 定義日期時間格式DateTimeFormatt…

【數據結構】—搜索二叉樹(C++實現,超詳細!)

&#x1f3ac;慕斯主頁&#xff1a;修仙—別有洞天 ??今日夜電波&#xff1a;消えてしまいそうです—真夜中 1:15━━━━━━?&#x1f49f;──────── 4:18 &#x1f504; ?? ? ??…

函數計算的新征程:使用 Laf 構建 AI 知識庫

Laf 已成功上架 Sealos 模板市場&#xff0c;可通過 Laf 應用模板來一鍵部署&#xff01; 這意味著 Laf 在私有化部署上的擴展性得到了極大的提升。 Sealos 作為一個功能強大的云操作系統&#xff0c;能夠秒級創建多種高可用數據庫&#xff0c;如 MySQL、PostgreSQL、MongoDB …

js實現獲取原生form表單的數據序列化表單以及將數組轉化為一個對象obj,將數組中的內容作為對象的key轉化為對象,對應的值轉換為對象對應的值

1.需求場景 哈嘍 大家好啊&#xff0c;今天遇到一個場景&#xff0c; js實現獲取原生form表單的數據序列化表單以及將數組轉化為一個對象obj&#xff0c;將數組中的內容作為對象的key轉化為對象&#xff0c;對應的值轉換為對象對應的值 數組對象中某個屬性的值&#xff0c;轉…

元宇宙現已開放!

在 2023 年 11 月 3 日 The Sandbox 首個全球創作者日上&#xff0c;The Sandbox 聯合創始人 Arthur Madrid 和 Sebastien Borget 宣布元宇宙已開放&#xff0c;已創作完整體驗的 LAND 持有者可以自行將體驗發布至 The Sandbox 地圖上。 精選速覽 LAND 持有者&#xff1a;如果…

在JVM中 判定哪些對象是垃圾?

目錄 垃圾的條件 1、引用計數法 2、可達性分析 3、強引用 4、軟引用 5、弱引用 6、虛引用 判斷垃圾的條件 在Java虛擬機&#xff08;JVM&#xff09;中&#xff0c;垃圾收集器負責管理內存&#xff0c;其中的垃圾收集算法用于確定哪些對象是垃圾&#xff0c;可以被回收…

VBA即用型代碼手冊之工作薄的關閉保存及創建

我給VBA下的定義&#xff1a;VBA是個人小型自動化處理的有效工具。可以大大提高自己的勞動效率&#xff0c;而且可以提高數據的準確性。我這里專注VBA,將我多年的經驗匯集在VBA系列九套教程中。 作為我的學員要利用我的積木編程思想&#xff0c;積木編程最重要的是積木如何搭建…

[Latex] Riemann 問題中的激波,接觸間斷,膨脹波的 Tikz 繪圖

Latex 代碼 \begin{figure}\begin{subfigure}[b]{0.32\textwidth}\centering\resizebox{\linewidth}{!}{\begin{tikzpicture}\coordinate (o) at (0,0);\coordinate (Si) at (2.5,2.5);\coordinate (x) at (1,0);\draw[->] (0,0) -- (3,0) node[right] {$x$};\draw[->] …

ArkTS-自定義組件學習

文章目錄 創建自定義組件頁面和自定義組件生命周期自定義組件和頁面的區別頁面生命周期(即被Entry修飾的組件)組件生命周期(即被Component修飾的組件) Builder裝飾器&#xff1a;自定義構建函數按引用傳遞參數按值傳遞參數 BuilderParam裝飾器&#xff1a;引用Builder函數 這個…

Python 將列表拼接為一個字符串,Python join

目錄 join方法的源碼&#xff1a; 列表數據為字符串 列表數據為數字 三引號也可以使用join join方法的源碼&#xff1a; def join(self, abNone, pqNone, rsNone): # real signature unknown; restored from __doc__"""Concatenate any number of strings.T…

harmonyos應用開發者高級認證考試部分答案

1只要使用端云一體化的云端資源就需要支付費用&#xff08;錯&#xff09; 2所有使用Component修飾的自定義組件都支持onPageShow&#xff0c;onBackPress和onPageHide生命周期函數。&#xff08;錯&#xff09; 3 HarmonyOS應用可以兼容OpenHarmony生態&#xff08;對&#…

一文讀懂如何安全地存儲密碼

目錄 引言 明文存儲 基本哈希存儲 加鹽哈希存儲 適應性哈希算法 密碼加密存儲 小結 引言 密碼是最常用的身份驗證手段&#xff0c;既簡單又高效。密碼安全是網絡安全的基石&#xff0c;對保護個人和組織信息的安全具有根本性的作用。然而有關密碼泄漏的安全問題一再發生…