C++雙向鏈表的增刪查改操作方法講解
一、什么是雙鏈表
雙向鏈表也叫雙鏈表,是鏈表的一種,它是單鏈表的升級版,與單鏈表不同的是,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。而單鏈表只有一個(gè)指針,指向后繼。
雙鏈表示意圖
首先創(chuàng)立一個(gè)結(jié)構(gòu)體,其中包含一個(gè)prev指針,一個(gè)val值以及一個(gè)next指針。如圖可以看出其中prev指針指向的是上一個(gè)結(jié)構(gòu)體,而next指針指向的是下一個(gè)結(jié)構(gòu)體。結(jié)構(gòu)體代碼
typedef int LTDataType; typedef struct ListNode { LTDataType _data; struct ListNode* _next; struct ListNode* _prev; }ListNode;
二、雙鏈表功能函數(shù)
1、創(chuàng)建返回鏈表的頭結(jié)點(diǎn)
ListNode* ListCreate() { ListNode* guard = (ListNode*)malloc(sizeof(ListNode)); if (guard == NULL) { perror("ListCreate"); exit(-1); } guard->_next = guard; guard->_prev = guard; return guard; }
2、雙向鏈表打印
void ListPrint(ListNode* pHead) { assert(pHead); ListNode* cur = pHead; while (cur->_next != pHead) { cur = cur->_next; printf("%d->", cur->_data); } printf("NULL\n"); return; }
3、雙向鏈表尾插
void ListPushBack(ListNode* pHead, LTDataType x) { ListNode* newnode = (ListNode*)malloc(sizeof(ListNode)); if (newnode == NULL) { perror("ListPushBack"); exit(-1); } newnode->_data = x; ListNode* cur = pHead->_prev; newnode->_next = pHead; newnode->_prev = cur; cur->_next = newnode; pHead->_prev = newnode; return; }
4、雙向鏈表尾刪
void ListPopBack(ListNode* pHead) { assert(pHead); ListNode* pre = pHead->_prev->_prev; free(pHead->_prev); pre->_next = pHead; pHead->_prev = pre; return; }
5、雙向鏈表頭插
void ListPushFront(ListNode* pHead, LTDataType x) { assert(pHead); ListNode* newnode = (ListNode*)malloc(sizeof(ListNode)); if (newnode == NULL) { perror("ListPushFront"); exit(-1); } newnode->_data = x; newnode->_next = pHead->_next; newnode->_prev = pHead; pHead->_next = newnode; newnode->_next->_prev = newnode; return; }
6、雙向鏈表頭刪
void ListPopFront(ListNode* pHead) { assert(pHead); ListNode* cur = pHead->_next->_next; free(pHead->_next); pHead->_next = cur; cur->_prev = pHead; return; }
7、雙向鏈表查找
ListNode* ListFind(ListNode* pHead, LTDataType x) { assert(pHead); ListNode* cur = pHead; while (cur->_next != pHead) { cur = cur->_next; if (cur->_data == x) return cur; } printf("Can't find.\n"); return NULL; }
8、雙向鏈表在pos的前面進(jìn)行插入
void ListInsert(ListNode* pos, LTDataType x) { assert(pos); ListNode* newnode = (ListNode*)malloc(sizeof(ListNode)); if (newnode == NULL) { perror("ListPushFront"); exit(-1); } ListNode* cur = pos->_prev; newnode->_next = pos; newnode->_prev = cur; pos->_prev = newnode; cur->_next = newnode; return; }
9、雙向鏈表刪除pos位置的節(jié)點(diǎn)
void ListErase(ListNode* pos) { ListNode* front = pos->_prev; ListNode* behind = pos->_next; free(pos); front->_next = behind; behind->_prev = front; return; }
10、雙向鏈表銷毀
void ListDestory(ListNode* pHead) { assert(pHead); while (pHead->_next != pHead) { pHead->_next = pHead->_next->_next; free(pHead->_next->_prev); pHead->_next->_prev = pHead; } return; }
到此這篇關(guān)于C++雙向鏈表的增刪查改操作方法講解的文章就介紹到這了,更多相關(guān)C++雙向鏈表的增刪查改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c/c++獲取系統(tǒng)時(shí)間函數(shù)的方法示例
這篇文章主要介紹了c/c++獲取系統(tǒng)時(shí)間函數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C語言數(shù)組長度的計(jì)算方法實(shí)例總結(jié)(sizeof與strlen)
數(shù)組一旦創(chuàng)建,程序運(yùn)行期間,長度不可改變,下面這篇文章主要給大家介紹了關(guān)于C語言數(shù)組長度的計(jì)算方法,主要利用的是sizeof與strlen,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06ubunt18.04LTS+vscode+anaconda3下的python+C++調(diào)試方法
這篇文章主要介紹了ubunt18.04LTS+vscode+anaconda3下的python+C++調(diào)試方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03C++命名空間using?namespace?std是什么意思
namespace中文意思是命名空間或者叫名字空間,傳統(tǒng)的C++只有一個(gè)全局的namespace,下面這篇文章主要給大家介紹了關(guān)于C++命名空間using?namespace?std是什么意思的相關(guān)資料,需要的朋友可以參考下2023-01-01C語言中時(shí)間戳轉(zhuǎn)換成時(shí)間字符串的方法
在PE格式里有個(gè)字段是文件的創(chuàng)建時(shí)間戳,我想把轉(zhuǎn)成字符串,今天小編給大家分享一段代碼,可以比較直觀的看出,需要的的朋友參考下2017-02-02