C++雙向鏈表的增刪查改操作方法講解
一、什么是雙鏈表
雙向鏈表也叫雙鏈表,是鏈表的一種,它是單鏈表的升級版,與單鏈表不同的是,它的每個數(shù)據(jù)結(jié)點中都有兩個指針,分別指向直接后繼和直接前驅(qū)。而單鏈表只有一個指針,指向后繼。
雙鏈表示意圖

首先創(chuàng)立一個結(jié)構(gòu)體,其中包含一個prev指針,一個val值以及一個next指針。如圖可以看出其中prev指針指向的是上一個結(jié)構(gòu)體,而next指針指向的是下一個結(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é)點
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的前面進行插入
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é)點
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++實現(xiàn)LeetCode(237.刪除鏈表的節(jié)點)
這篇文章主要介紹了C++實現(xiàn)LeetCode(237.刪除鏈表的節(jié)點),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
C語言數(shù)組長度的計算方法實例總結(jié)(sizeof與strlen)
數(shù)組一旦創(chuàng)建,程序運行期間,長度不可改變,下面這篇文章主要給大家介紹了關(guān)于C語言數(shù)組長度的計算方法,主要利用的是sizeof與strlen,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06
ubunt18.04LTS+vscode+anaconda3下的python+C++調(diào)試方法
這篇文章主要介紹了ubunt18.04LTS+vscode+anaconda3下的python+C++調(diào)試方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
C++命名空間using?namespace?std是什么意思
namespace中文意思是命名空間或者叫名字空間,傳統(tǒng)的C++只有一個全局的namespace,下面這篇文章主要給大家介紹了關(guān)于C++命名空間using?namespace?std是什么意思的相關(guān)資料,需要的朋友可以參考下2023-01-01

