利用C++實(shí)現(xiàn)雙鏈表基本接口示例代碼
鏈表
鏈表是一種物理存儲(chǔ)單元上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。鏈表由一系列結(jié)點(diǎn)(鏈表中每一個(gè)元素稱為結(jié)點(diǎn))組成,結(jié)點(diǎn)可以在運(yùn)行時(shí)動(dòng)態(tài)生成。每個(gè)結(jié)點(diǎn)包括兩個(gè)部分:一個(gè)是存儲(chǔ)數(shù)據(jù)元素的數(shù)據(jù)域,另一個(gè)是存儲(chǔ)下一個(gè)結(jié)點(diǎn)地址的指針域。 相比于線性表順序結(jié)構(gòu),鏈表比較方便插入和刪除操作。
本文主要給大家介紹了關(guān)于C++實(shí)現(xiàn)雙鏈表基本接口的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹吧。
首先先簡(jiǎn)單通過圖示區(qū)分單鏈表和雙鏈表的結(jié)構(gòu)差異:

單鏈表的基本接口實(shí)現(xiàn)可參考:單鏈表簡(jiǎn)單實(shí)現(xiàn)
接下來就是雙鏈表的基本接口實(shí)現(xiàn):
#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
DataType _data;
ListNode(DataType x)
:_next(NULL)
, _prev(NULL)
, _data(x)
{}
};
typedef ListNode Node;
class List
{
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const List& l)
:_head(NULL)
,_tail(NULL)
{
Copy(l);
}
void Copy(const List& l)
{
Node* cur = l._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
List& operator=(const List& l)
{
Destory();
Copy(l);
return *this;
}
~List()
{
Destory();
}
void Destory()
{
if (_head)
{
Node* cur = _head;
while (_head)
{
cur = _head;
_head = _head->_next;
delete cur;
}
_head = _tail = NULL;
}
}
void PushBack(DataType x)
{
if (_head == NULL)
{
Node* tmp = new Node(x);
tmp->_next = tmp->_prev = NULL;
_head = _tail = tmp;
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete tmp;
}
}
void PushFront(DataType x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
tmp->_next = _head;
_head->_prev = tmp;
_head = _head->_prev;
}
}
void PopFront()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _head;
_head = _head->_next;
delete tmp;
_head->_prev = NULL;
}
}
Node* Find(DataType x)
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
return cur;
cur = cur->_next;
}
return NULL;
}
// 在pos的前面插入x
void Insert(Node* pos, DataType x)
{
assert(pos);
if ((pos == 0) || (pos->_prev == NULL))
{
PushFront(x);
}
else
{
Node* font = pos->_prev;
Node* tmp = new Node(x);
tmp->_prev = font;
tmp->_next = pos;
font->_next = tmp;
pos->_prev = tmp;
}
}
//刪除pos位置的元素
void Erase(Node* pos)
{
assert(pos);
if ((pos == 0) || (pos->_prev == NULL))
{
PopFront();
}
else if (pos->_next == NULL)
{
PopBack();
}
else
{
Node* font = pos->_prev;
Node* last = pos->_next;
font->_next = last;
last->_prev = font;
delete pos;
}
}
//逆序整個(gè)雙鏈表
void Reverse()
{
Node* cur = _head;
while (cur)
{
swap(cur->_next,cur->_prev);
cur = cur->_prev;
}
swap(_head, _tail);
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << endl;
}
private:
Node* _head;
Node* _tail;
};
注:在一些操作實(shí)現(xiàn)時(shí),一定要要考慮清楚各種情況,再進(jìn)行情況的分類盡量提高代碼的復(fù)用程度。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持
- C++ 雙鏈表的基本操作(詳解)
- C數(shù)據(jù)結(jié)構(gòu)之雙鏈表詳細(xì)示例分析
- C/C++ 雙鏈表之逆序的實(shí)例詳解
- C語言雙向鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
- C語言實(shí)現(xiàn)雙向鏈表
- C語言之雙向鏈表詳解及實(shí)例代碼
- C語言數(shù)據(jù)結(jié)構(gòu) 雙向鏈表的建立與基本操作
- C語言中雙向鏈表和雙向循環(huán)鏈表詳解
- C語言 數(shù)據(jù)結(jié)構(gòu)雙向鏈表簡(jiǎn)單實(shí)例
- C語言實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)和雙向鏈表操作
- C語言實(shí)現(xiàn)的雙鏈表功能完整示例
相關(guān)文章
C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C語言去除相鄰重復(fù)字符函數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,實(shí)現(xiàn)去重字符串相鄰重復(fù)的字符,不相鄰的不用去重的功能,需要的朋友可以參考下2017-08-08
C語言實(shí)現(xiàn)控制臺(tái)版貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了c語言貪吃蛇控制臺(tái)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)
這篇文章介紹了C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級(jí)簡(jiǎn)單)
這篇文章主要介紹了OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級(jí)簡(jiǎn)單),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
VScode配置C語言環(huán)境完整版(親測(cè)可用)
這篇文章主要介紹了VScode配置C語言環(huán)境完整版,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

