C++實(shí)現(xiàn)雙向鏈表(List)
list是C++容器類中的“順序存儲(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語言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器詳解
這篇文章主要給大家介紹了關(guān)于如何利用C語言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C語言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11STL priority_queue(優(yōu)先隊(duì)列)詳解
這篇文章主要介紹了 STL priority_queue(優(yōu)先隊(duì)列)詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10C語言實(shí)現(xiàn)文件內(nèi)容按行隨機(jī)排列的算法示例
這篇文章主要介紹了C語言實(shí)現(xiàn)文件內(nèi)容按行隨機(jī)排列的算法,涉及C語言字符串、數(shù)組遍歷與隨機(jī)數(shù)相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09解析C++編程中virtual聲明的虛函數(shù)以及單個(gè)繼承
這篇文章主要介紹了C++編程中virtual聲明的虛函數(shù)以及單個(gè)繼承,剖析虛函數(shù)和單個(gè)基類所能夠繼承的成員,要的朋友可以參考下2016-01-01C++實(shí)現(xiàn)LeetCode(41.首個(gè)缺失的正數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(41.首個(gè)缺失的正數(shù)),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07基于Opencv實(shí)現(xiàn)雙目攝像頭拍照程序
這篇文章主要為大家詳細(xì)介紹了基于Opencv實(shí)現(xiàn)雙目攝像頭拍照程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04