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

首先創(chuàng)立一個結(jié)構體,其中包含一個prev指針,一個val值以及一個next指針。如圖可以看出其中prev指針指向的是上一個結(jié)構體,而next指針指向的是下一個結(jié)構體。結(jié)構體代碼
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;
}
到此這篇關于C++雙向鏈表的增刪查改操作方法講解的文章就介紹到這了,更多相關C++雙向鏈表的增刪查改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言表達式求值中類型轉(zhuǎn)換和優(yōu)先級等問題詳解
表達式求值是一個常見的問題,可以用C語言實現(xiàn),下面這篇文章主要給大家介紹了關于C語言表達式求值中類型轉(zhuǎn)換和優(yōu)先級等問題的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
C語言實現(xiàn)將字符串轉(zhuǎn)換為數(shù)字的方法
這篇文章主要介紹了C語言實現(xiàn)將字符串轉(zhuǎn)換為數(shù)字的方法,涉及系統(tǒng)函數(shù)atoi()函數(shù)的使用技巧,需要的朋友可以參考下2014-12-12

