C++實(shí)現(xiàn)雙向鏈表(List)
list是C++容器類(lèi)中的“順序存儲(chǔ)結(jié)構(gòu)”所包含的一種結(jié)構(gòu)。list是非連續(xù)存儲(chǔ)結(jié)構(gòu),具有雙鏈表結(jié)構(gòu),支持前向/后向遍歷,且支持高效的隨機(jī)刪除/插入。

實(shí)現(xiàn)代碼如下:
**list.h**
#pragma once
#include<stdio.h>
#include<assert.h>
#include<iostream>
using namespace std;
typedef int DataType;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
DataType _data;
ListNode(DataType x)
:_data(x)
, _next(NULL)
, _prev(NULL)
{}
};
**test.cpp**
#define _CRT_SECURE_NO_WARNINGS 1
#include "list.h"
class List{
typedef ListNode Node;
public:
List()
:_head(new Node(DataType())){
_head->_next = _head;
_head->_prev = _head;
}
List(const List& l)
:_head(new Node(DataType())){
_head->_next = _head;
_head->_prev = _head;
Node* cur = l._head->_next;
while (cur!=l._head){
PushBack(cur->_data);
cur = cur->_next;
}
}
List& operator=(const List& l){
if (this != &l){
//swap(_head, l._head);
}
return *this;
}
~List(){
Node* cur = _head->_next;
while (cur != _head){
Node* next= cur->_next;
delete cur;
cur = next;
}
delete _head;
_head = NULL;
}
void PushBack(DataType x){
Node* prev = _head->_prev;
Node* new_node = new Node(x);
prev->_next = new_node;
new_node->_prev = prev;
_head->_prev = new_node;
new_node->_next = _head;
}
void PushFront(DataType x){//插在頭結(jié)點(diǎn)之后
Node* cur = _head->_next;
Node* new_node = new Node(x);
new_node->_next = cur;
cur->_prev = new_node;
new_node->_prev = _head;
_head->_next = new_node;
}
void PopBack(){
Node* delete_node = _head->_prev;
Node* cur = delete_node->_prev;
_head->_prev = cur;
cur->_next = _head;
delete delete_node;
}
void PopFront(){
Node* delete_node = _head->_next;
Node* cur = delete_node->_next;
_head->_next = cur;
cur->_prev = _head;
delete delete_node;
}
Node* Find(DataType x){
Node* to_find = _head->_next;
while (to_find != _head){
if (to_find->_data == x){
return to_find;
}
to_find = to_find->_next;
}
return NULL;
}
void Insert(Node* pos, DataType x){//在pos位置前插入數(shù)據(jù)
assert(pos);
Node* new_node = new Node(x);
Node* prev = pos->_prev;
new_node->_next = pos;
pos->_prev = new_node;
new_node->_prev = prev;
prev->_next = new_node;
}
void Erase(Node* pos){
assert(pos);
Node* prev = pos->_prev;
Node* next = pos->_next;
prev->_next = next;
next->_prev = prev;
delete pos;
}
void Print() const{
Node* cur = _head->_next;
cout <<" _head->";
while (cur!= _head){
cout << cur->_data << "->";
cur = cur->_next;
}
cout << endl;
Node* pos = _head->_prev;
while (pos != _head){
cout << pos->_data << "<-";
pos = pos->_prev;
}
cout << "_head" << endl;
}
private:
Node* _head;
};
void TestList(){
List L;
L.PushBack(1);
L.PushBack(2);
L.PushBack(4);
L.PushBack(5);
L.PopBack();
L.Print();
ListNode* pos = L.Find(1);
printf("pos->data:%d[%p]\n",pos->_data,pos);
pos = L.Find(2);
printf("pos->data:%d[%p]\n", pos->_data, pos);
pos = L.Find(4);
printf("pos->data:%d[%p]\n", pos->_data, pos);
printf("\n");
L.Insert(pos, 3);
L.Print();
L.Erase(pos);
L.Print();
printf("\n");
List L1;
L1.PushFront(4);
L1.PushFront(3);
L1.PushFront(2);
L1.PushFront(1);
L1.Print();
L1.PopFront();
L1.Print();
}
int main(){
TestList();
return 0;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c++ priority_queue用法入門(mén)超詳細(xì)教程
priority_queue即優(yōu)先級(jí)隊(duì)列,它的使用場(chǎng)景很多,它底層是用大小根堆實(shí)現(xiàn)的,可以用log(n)的時(shí)間動(dòng)態(tài)地維護(hù)數(shù)據(jù)的有序性,這篇文章主要介紹了c++ priority_queue用法入門(mén)超詳細(xì)教程,需要的朋友可以參考下2023-12-12
C++/GoLang如何實(shí)現(xiàn)自底向上的歸并排序
這篇文章主要給大家介紹了關(guān)于C++/GoLang如何實(shí)現(xiàn)自底向上的歸并排序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
C語(yǔ)言利用cJSON解析JSON格式全過(guò)程
cJSON是用于解析json格式字符串的一套api,非常好用,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言利用cJSON解析JSON格式的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
離線安裝visual?studio2022+QT5.12的實(shí)現(xiàn)步驟
近期有需求離線配置C++與QT環(huán)境,本文主要介紹了離線安裝visualstudio2022+QT5.12的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
OpenCV實(shí)現(xiàn)圖像去噪算法的步驟詳解
這篇文章主要為大家介紹了OpenCV中圖像去噪算法的原理,文中通過(guò)示例為大家詳細(xì)講解了圖像去噪算法的使用,感興趣的小伙伴可以了解一下2022-06-06

