模擬實現雙向帶頭迴圈鍊錶list

2021-10-07 20:27:49 字數 2954 閱讀 9497

#

include

using

namespace std;

//list實現:雙向帶頭迴圈鍊錶

template

<

class

t>

struct

listnode};

迭代器型別

//template

//struct listiterator

////

// //*iterator

// t& operator*()

// //

// //++iterator

// listiterator& operator++()

// //

// bool operator!=(const listiterator& it)

// //

// //iterator++

// listiteratoroperator++(int)

// //

// //使用場景:t為自定義型別,且包含多個成員

// //通過->訪問每乙個成員

// t* operator->()

// //

// bool operator==(const listiterator& it)

// //

// listiterator& operator--()

// //};

const迭代器

//template

//struct constlistiterator

////

// //++iterator

// constlistiterator& operator++()

// //

// bool operator!=(const constlistiterator& it)

// //

// //iterator++

// constlistiteratoroperator++(int)

// //

// //*iterator

// const t& operator*()

// //

// //使用場景:t為自定義型別,且包含多個成員

// //通過->訪問每乙個成員

// const t* operator->()

// //

// bool operator==(const constlistiterator& it)

// //

// constlistiterator& operator--()

// //};

//庫中迭代器的實現

template

<

classt,

class

ref,

class

ptr>

struct

listiterator

//*iterator

ref operator*(

)//++iterator

self&

operator++(

)bool

operator!=(

const self& it)

//iterator++

self operator++(

int)

//使用場景:t為自定義型別,且包含多個成員

//通過->訪問每乙個成員

ptr operator

->()

bool

operator==(

const self& it)

self&

operator--(

)};template

<

class

t>

class

list

iterator end()

list()

:_header

(new

node()

)list

(size_t n,

const t& val =t(

)):_header

(new

node()

)}template

<

class

inputiterator

>

list

(inputiterator first, inputiterator last)

:_header

(new

node()

)}void

pushback

(const t& val)

void

pushfront

(const t& val)

//迭代器指向的節點釋放,迭代器失效

//更新迭代器,返回值iterator指向被刪除元素的下乙個位置

iterator erase

(iterator pos)

return pos;

}void

popback()

void

popfront()

void

insert

(iterator pos,

const t& val)

~list()

delete _header;

_header =

nullptr;}

}//深拷貝

list

(const list

& lst)

:_header

(new

node()

)}//深拷貝

list

&operator=(

const list

& lst)

private

: listnode

* _header;

};

c 模擬實現帶頭迴圈雙向鍊錶

思路 通單鏈表相似,不過該鍊錶節點包含兩個指標域和乙個資料域,乙個指標域指向前乙個,乙個指標域指向後乙個,其他通單鏈表相似 test.h define crt secure no warnings 1 2 帶頭雙向迴圈鍊錶增刪查改實現 typedef int ltdatatype typedef s...

帶頭雙向迴圈鍊錶

首先,我們來看一下帶頭雙向迴圈鍊錶的結構 目錄 帶頭雙向迴圈鍊錶結點的定義 相關操作介面 1 初始化 獲取乙個結點 2 銷毀鍊錶 3 尾插 4 頭插 5 指定元素查詢 6 任意位置插入 7 尾刪 8 頭刪 9 任意位置刪除 10 列印鍊錶 附上完整 typedef int datatype type...

帶頭雙向迴圈鍊錶

帶頭雙向迴圈鍊錶的增刪查改實現 帶頭 雙向 迴圈鍊錶增刪查改實現 typedef int ltdatatype typedef struct listnode listnode 建立乙個新節點 listnode buylistnode ltdatatype x 建立返回鍊錶的頭節點 listnode...