日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

双向链表数据结构

發布時間:2025/4/5 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双向链表数据结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 雙向鏈表數據結構
      • 1.1 鏈表結構定義
      • 1.2 鏈表操作實現

1 雙向鏈表數據結構

1.1 鏈表結構定義

對于雙向鏈表的通常做法:

我們可以使用更好的方案:


思考一個問題:已知父結構,如何訪問特定結點?


1.2 鏈表操作實現

鏈表的定義如下:

#ifndef TLIB_H #define TLIB_H// 標準頭文件,里面包含了常用的類型定義,如uint32_t #include <stdint.h> // tinyOS鏈表的結點類型 typedef struct _tNode {// 該結點的前一個結點struct _tNode * preNode;// 該結點的后一個結點struct _tNode * nextNode; }tNode;/********************************************************************************************************** ** Function name : tNodeInit ** Descriptions : 初始化結點類型 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tNodeInit (tNode * node);// tinyOS鏈表類型 typedef struct _tList { // 該鏈表的頭結點tNode headNode;// 該鏈表中所有結點數量uint32_t nodeCount; }tList;/********************************************************************************************************** ** Function name : tNodeParent ** Descriptions : 獲取結點所在的父struct結構首地址 ** parameters : 無 ** Returned value : 父struct結構首地址 ***********************************************************************************************************/ #define tNodeParent(node, parent, name) (parent *)((uint32_t)node - (uint32_t)&((parent *)0)->name)/********************************************************************************************************** ** Function name : tListInit ** Descriptions : 鏈表初始化 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tListInit (tList * list);/********************************************************************************************************** ** Function name : tListCount ** Descriptions : 返回鏈表中結點的數量 ** parameters : 無 ** Returned value : 結點數量 ***********************************************************************************************************/ uint32_t tListCount (tList * list);/********************************************************************************************************** ** Function name : tListFirst ** Descriptions : 返回鏈表的首個結點 ** parameters : list 查詢的鏈表 ** Returned value : 首個結點,如果鏈表為空,則返回0 ***********************************************************************************************************/ tNode * tListFirst (tList * list);/********************************************************************************************************** ** Function name : tListLast ** Descriptions : 返回鏈表的最后一個結點 ** parameters : list 查詢的鏈表 ** Returned value : 最后的結點,如果鏈表為空,則返回0 ***********************************************************************************************************/ tNode * tListLast (tList * list);/********************************************************************************************************** ** Function name : tListPre ** Descriptions : 返回鏈表中指定結點的前一結點 ** parameters : list 查詢的鏈表 ** parameters : node 參考結點 ** Returned value : 前一結點結點,如果結點沒有前結點(鏈表為空),則返回0 ***********************************************************************************************************/ tNode * tListPre (tList * list, tNode * node);/********************************************************************************************************** ** Function name : tListNext ** Descriptions : 返回鏈表中指定結點的后一結點 ** parameters : list 查詢的鏈表 ** parameters : node 參考結點 ** Returned value : 后一結點結點,如果結點沒有前結點(鏈表為空),則返回0 ***********************************************************************************************************/ tNode * tListNext (tList * list, tNode * node);/********************************************************************************************************** ** Function name : tListRemoveAll ** Descriptions : 移除鏈表中的所有結點 ** parameters : list 待清空的鏈表 ** Returned value : 無 ***********************************************************************************************************/ void tListRemoveAll (tList * list);/********************************************************************************************************** ** Function name : tListAddFirst ** Descriptions : 將指定結點添加到鏈表的頭部 ** parameters : list 待插入鏈表 ** parameters : node 待插入的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListAddFirst (tList * list, tNode * node);/********************************************************************************************************** ** Function name : tListAddLast ** Descriptions : 將指定結點添加到鏈表的末尾 ** parameters : list 待插入鏈表 ** parameters : node 待插入的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListAddLast (tList * list, tNode * node);/********************************************************************************************************** ** Function name : tListRemoveFirst ** Descriptions : 移除鏈表中的第1個結點 ** parameters : list 待移除鏈表 ** Returned value : 如果鏈表為空,返回0,否則的話,返回第1個結點 ***********************************************************************************************************/ tNode * tListRemoveFirst (tList * list);/********************************************************************************************************** ** Function name : tListInsertAfter ** Descriptions : 將指定的結點插入到某個結點后面 ** parameters : list 待插入的鏈表 ** parameters : nodeAfter 參考結點 ** parameters : nodeToInsert 待插入的結構 ** Returned value : 無 ***********************************************************************************************************/ void tListInsertAfter (tList * list, tNode * nodeAfter, tNode * nodeToInsert);/********************************************************************************************************** ** Function name : tListRemove ** Descriptions : 將指定結點從鏈表中移除 ** parameters : list 待移除的鏈表 ** parameters : node 待移除的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListRemove (tList * list, tNode * node);#endif /* TLIB_H */

鏈表相關的操作實現如下:

/*************************************** Copyright (c)****************************************************** ** File name : tList.c ** Latest modified Date : 2016-06-01 ** Latest Version : 0.1 ** Descriptions : tinyOS所用的雙向鏈表數據結構。 ** **-------------------------------------------------------------------------------------------------------- ** Created by : 01課堂 lishutong ** Created date : 2016-06-01 ** Version : 1.0 ** Descriptions : The original version ** **-------------------------------------------------------------------------------------------------------- ** Copyright : 版權所有,禁止用于商業用途 ** Author Blog : http://ilishutong.com **********************************************************************************************************/ #include "tLib.h"/********************************************************************************************************** ** Function name : tNodeInit ** Descriptions : 初始化結點類型 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tNodeInit (tNode * node) {node->nextNode = node;node->preNode = node; }// 以下是簡化代碼編寫添加的宏 #define firstNode headNode.nextNode #define lastNode headNode.preNode/********************************************************************************************************** ** Function name : tListInit ** Descriptions : 鏈表初始化 ** parameters : 無 ** Returned value : 無 ***********************************************************************************************************/ void tListInit (tList * list) {list->firstNode = &(list->headNode);list->lastNode = &(list->headNode);list->nodeCount = 0; }/********************************************************************************************************** ** Function name : tListCount ** Descriptions : 返回鏈表中結點的數量 ** parameters : 無 ** Returned value : 結點數量 ***********************************************************************************************************/ uint32_t tListCount (tList * list) {return list->nodeCount; }/********************************************************************************************************** ** Function name : tListFirst ** Descriptions : 返回鏈表的首個結點 ** parameters : list 查詢的鏈表 ** Returned value : 首個結點,如果鏈表為空,則返回0 ***********************************************************************************************************/ tNode * tListFirst (tList * list) {tNode * node = (tNode *)0;if (list->nodeCount != 0) {node = list->firstNode;} return node; }/********************************************************************************************************** ** Function name : tListLast ** Descriptions : 返回鏈表的最后一個結點 ** parameters : list 查詢的鏈表 ** Returned value : 最后的結點,如果鏈表為空,則返回0 ***********************************************************************************************************/ tNode * tListLast (tList * list) {tNode * node = (tNode *)0;if (list->nodeCount != 0) {node = list->lastNode;} return node; }/********************************************************************************************************** ** Function name : tListPre ** Descriptions : 返回鏈表中指定結點的前一結點 ** parameters : list 查詢的鏈表 ** parameters : node 參考結點 ** Returned value : 前一結點結點,如果結點沒有前結點(鏈表為空),則返回0 ***********************************************************************************************************/ tNode * tListPre (tList * list, tNode * node) {if (node->preNode == node) {return (tNode *)0;} else {return node->preNode;} }/********************************************************************************************************** ** Function name : tListnextNode ** Descriptions : 返回鏈表中指定結點的后一結點 ** parameters : list 查詢的鏈表 ** parameters : node 參考結點 ** Returned value : 后一結點結點,如果結點沒有前結點(鏈表為空),則返回0 ***********************************************************************************************************/ tNode * tListNext (tList * list, tNode * node) {if (node->nextNode == node) {return (tNode *)0;}else {return node->nextNode;} }/********************************************************************************************************** ** Function name : tListRemoveAll ** Descriptions : 移除鏈表中的所有結點 ** parameters : list 待清空的鏈表 ** Returned value : 無 ***********************************************************************************************************/ void tListRemoveAll (tList * list) {uint32_t count;tNode * nextNode;// 遍歷所有的結點nextNode = list->firstNode;for (count = list->nodeCount; count != 0; count-- ){// 先紀錄下當前結點,和下一個結點// 必須紀錄下一結點位置,因為在后面的代碼中當前結點的next會被重置tNode * currentNode = nextNode;nextNode = nextNode->nextNode;// 重置結點自己的信息currentNode->nextNode = currentNode;currentNode->preNode = currentNode;}list->firstNode = &(list->headNode);list->lastNode = &(list->headNode);list->nodeCount = 0; }/********************************************************************************************************** ** Function name : tListAddFirst ** Descriptions : 將指定結點添加到鏈表的頭部 ** parameters : list 待插入鏈表 ** parameters : node 待插入的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListAddFirst (tList * list, tNode * node) {node->preNode = list->firstNode->preNode;node->nextNode = list->firstNode;list->firstNode->preNode = node;list->firstNode = node;list->nodeCount++; }/********************************************************************************************************** ** Function name : tListAddLast ** Descriptions : 將指定結點添加到鏈表的末尾 ** parameters : list 待插入鏈表 ** parameters : node 待插入的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListAddLast (tList * list, tNode * node) {node->nextNode = &(list->headNode);node->preNode = list->lastNode;list->lastNode->nextNode = node;list->lastNode = node;list->nodeCount++; }/********************************************************************************************************** ** Function name : tListRemoveFirst ** Descriptions : 移除鏈表中的第1個結點 ** parameters : list 待移除鏈表 ** Returned value : 如果鏈表為空,返回0,否則的話,返回第1個結點 ***********************************************************************************************************/ tNode * tListRemoveFirst (tList * list) {tNode * node = (tNode *)0;if( list->nodeCount != 0 ){node = list->firstNode;node->nextNode->preNode = &(list->headNode);list->firstNode = node->nextNode;list->nodeCount--;}return node; }/********************************************************************************************************** ** Function name : tListInsertAfter ** Descriptions : 將指定的結點插入到某個結點后面 ** parameters : list 待插入的鏈表 ** parameters : nodeAfter 參考結點 ** parameters : nodeToInsert 待插入的結構 ** Returned value : 無 ***********************************************************************************************************/ void tListInsertAfter (tList * list, tNode * nodeAfter, tNode * nodeToInsert) {nodeToInsert->preNode = nodeAfter;nodeToInsert->nextNode = nodeAfter->nextNode;nodeAfter->nextNode->preNode = nodeToInsert;nodeAfter->nextNode = nodeToInsert;list->nodeCount++; }/********************************************************************************************************** ** Function name : tListRemove ** Descriptions : 將指定結點從鏈表中移除 ** parameters : list 待移除的鏈表 ** parameters : node 待移除的結點 ** Returned value : 無 ***********************************************************************************************************/ void tListRemove (tList * list, tNode * node) {node->preNode->nextNode = node->nextNode;node->nextNode->preNode = node->preNode;list->nodeCount--; }

參考資料:

  • 【李述銅】從0到1自己動手寫嵌入式操作系統
  • 總結

    以上是生活随笔為你收集整理的双向链表数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: www.伊人| 国产精品人成 | av字幕在线 | 中文字幕视频免费 | 国产精品久久久久久久午夜 | 精品国产18久久久久久 | 三上悠亚在线观看一区二区 | 免费在线一区二区 | a网址| 在线播放毛片 | 免费观看全黄做爰的视频 | 中文字幕免费高清在线 | 亚洲一区二区精品视频 | 狠狠爱夜夜操 | 中文字幕一区二区三区精彩视频 | 东北少妇露脸无套对白 | 日本精品久久久久久 | 91精品国产乱码久久久 | 不卡中文字幕 | 射射综合网| 女同性做受全过程动图 | 久久青青草原亚洲av无码麻豆 | 一级黄色短视频 | 蜜桃久久久 | 精品人妻伦一二三区久久 | 性欧美在线视频观看 | 一区二区三区在线看 | 亚洲熟女www一区二区三区 | 一道本在线 | 性一交一乱一乱一视频 | 涩涩屋视频在线观看 | 精品在线你懂的 | 亚洲性视频在线 | 亚洲资源网 | 国产精品色在线 | 丰满岳乱妇在线观看中字无码 | a极黄色片 | 国产妇女乱一性一交 | 色综合啪啪 | 视频一区二区三区在线 | 暴操白虎 | 免费激情视频网站 | 中文字幕一区二区人妻在线不卡 | 国产成人麻豆免费观看 | 欧洲a级片 | 亚洲第一第二区 | 久久综合鬼色 | 老外毛片| 熊出没之冬日乐翻天免费高清观看 | 免费黄色在线网站 | 亚洲AV无码成人精品区明星换面 | 理论视频在线观看 | 亚洲视频一区二区三区四区 | 老色批永久免费网站www | 无码精品视频一区二区三区 | 曰本毛片 | 日韩精品一区二区亚洲av性色 | 欧美黄色成人 | 天天操天天操天天操天天 | 日本美女日批视频 | 香蕉福利视频 | 国产最新毛片 | 天天操天天操天天操天天 | aa级黄色片 | 国产三级视频在线播放 | 丰满雪白极品少妇流白浆 | 午夜黄网 | 日韩精品一区二区三区中文字幕 | 日本三级在线视频 | 自拍偷拍亚洲精品 | 禁网站在线观看免费视频 | 老外一级黄色片 | 中文字幕国产一区二区 | 一级 黄 色 片69 | 日韩区一区二 | 一级片视频在线观看 | av高清一区二区 | 国产第十页 | 国产精品福利一区 | 黄网免费视频 | 亚洲最大黄色 | xxxxwww一片 | 在线观看毛片视频 | 婷婷激情视频 | 日本一级黄色大片 | 色中文 | 国产一区二区三区中文字幕 | 五月婷婷激情小说 | 一区二区三区高清在线 | 69久久久久久 | 日韩影视一区二区三区 | 在线观看精品国产 | 免费黄网站在线看 | 怡春院在线视频 | 麻豆videos| 黑人与日本少妇 | 久一在线| 无码少妇一区二区 | 人妻在线一区二区 |