十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹了C++如何模擬實(shí)現(xiàn)list迭代器,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),靈臺企業(yè)網(wǎng)站建設(shè),靈臺品牌網(wǎng)站建設(shè),網(wǎng)站定制,靈臺網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,靈臺網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
C++ 模擬實(shí)現(xiàn)list(迭代器)
實(shí)現(xiàn)代碼:
#pragma once; #include#include #include using namespace std; template struct __ListNode { T _data; __ListNode * _next; __ListNode * _prev; __ListNode(const T& x) :_data(x) ,_next(NULL) ,_prev(NULL) { } }; template struct __ListIterator { typedef __ListNode Node; typedef __ListIterator Self; __ListIterator(Node* node) :_node(node) { } Ref operator*() { return _node->_data; } Ptr operator->() { return &(_node->_data) } Self& operator++() { _node=_node->_next; return *this; } Self& operator--() { _node=_node->_prev; return *this; } Self operator++(int) { Self tmp=_node; _node=_node->_next; //return tmp; return Self(tmp) } Self operator--(int) { Self tmp=(*this); _node=_node->_prev; return tmp; } bool operator!=(const Self& s) const { return this->_node!=s._node; } bool operator==(const Self& s) const { return this->_node==s._node; } Node* _node; }; template struct List { typedef __ListNode Node; public: typedef __ListIterator Iterator; typedef __ListIterator ConstIterator; Node* GetNode(const T& x) { return new Node(x); } List() { _head=GetNode(T()); _head->_next=_head; _head->_prev=_head; } Iterator Begin() { return Iterator(_head->_next); } Iterator End() { return Iterator(_head); } ConstIterator Begin() const { return ConstIterator(_head->_next); } ConstIterator End() const { return ConstIterator(_head); } void PushBack(const T& x) { /* Node* _tail=_head->_prev; Node* tmp=GetNode(x); _tail->_next=tmp; tmp->_prev=_tail; tmp->_next=_head; _head->_prev=tmp;*/ Insert(End(),x); } void PopBack() { /* assert(_head->_prev ); Node* tail=_head->_prev; Node* prev=tail->_prev; Node* next=tail->_next; prev->_next=next; next->_prev=prev; delete tail;*/ Erase(--End()); } void PushFront(const T& x) { /*assert(_head) Node* tmp=GetNode(x); Node* next=_head->_next; _head->_next=tmp; tmp->_prev=_head; tmp->_next=next; next->_prev=tmp;*/ Insert(Begin(),x); } void PopFront() { /*assert(_head->_next); Node* tmp=_head->_next; Node* next=tmp->_next; _head->_next= next; next->_prev=_head; delete tmp;*/ Erase(Begin()); } Iterator Insert(Iterator pos, const T& x) { assert(pos._node); Node* tmp=GetNode(x); Node* cur=pos._node; Node* prev=cur->_prev; prev->_next=tmp; tmp->_prev=prev; tmp->_next=cur; cur->_prev=tmp; return tmp; } Iterator Erase(Iterator pos) { assert(pos._node && pos._node!=NULL); Node* tmp=pos._node; Node* next=tmp->_next; Node* prev=tmp->_prev; next->_prev=prev; prev->_next=next; delete tmp; return Iterator(next); } protected: Node* _head; }; void PrintList(const List & l) { List ::ConstIterator It=l.Begin(); while(It!=l.End()) { cout<<*It<<" "; ++It; } cout< l2; l2.PushBack(1); l2.PushBack(2); l2.PushBack(3); l2.PushBack(4); l2.PopBack(); l2.PopBack(); l2.PopBack(); l2.PopBack(); l2.PopBack(); PrintList(l2); } void TestList3() { List l3; l3.PushFront(1); l3.PushFront(2); l3.PushFront(3); l3.PushFront(4); l3.PopFront(); l3.PopFront(); l3.PopFront(); PrintList(l3); }
#include "List.h"
int main()
{
//TestList1();
//TestList2();
TestList3();
return 0;
}感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C++如何模擬實(shí)現(xiàn)list迭代器”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!