模擬實現list即將list中的函數模擬實現,一樣也分爲五部分:構造與析構、容量、迭代器、元素訪問、元素修改。
須要注意的問題是list中的迭代器,與vector中不一樣的是list不是順序結構,因此咱們要對迭代器進行封裝,其使用規則也要在這個封裝中自定義給出。vector中的使用原生態指針便可。
代碼以下:ios
#include<iostream> using namespace std; namespace mine//本身定義的命名空間,爲了防止與庫中的list衝突 { template <class T>//定義了一個節點類 class ListNode { public: ListNode(const T &data=T())//沒有傳參的話調用T類型對象的默認構造函數 :_pPre(nullptr) , _pNext(nullptr) , _data(data) { } ListNode<T>*_pPre; ListNode<T>*_pNext; T _data; }; template<class T> class Iterator { public: typedef ListNode<T> Node;//對節點類重命名,方便後面的使用 typedef Iterator<T> Self; Iterator(Node *cur)//構造函數 :_pCur(cur) { } T& operator *()//按照指針的方式進行引用 { return _pCur->_data; } T *operator ->() { return &(_pCur->_data); } Self& operator++()//前置++ { _pCur = _pCur->_pNext; return *this; } Self& operator++(int)//後置++ { Self tmp(*this); _pCur = _pCur->_pNext; return *tmp; } Self& operator--()//前置-- { _pCur = _pCur->_pPre; return *this; } Self &operator--(int)//後置-- { Self tmp(*this); _pCur = _pCur->_pPre; return *tmp; } bool operator ==(const Self & l) { if (l._pCur==_pCur) { return true; } return false; } bool operator !=(const Self & l) { return !(_pCur == l._pCur); } Node * _pCur; }; template <class T>//list類 class list { public: typedef ListNode<T> Node; typedef Iterator<T> iterator; public: ////////////////////////////////////////////////////////////// //構造與析構 typedef ListNode<T> Node;//對節點類型重命名,使用起來更加方便 list()//默認構造函數,只建立一個頭節點 { CreatHead(); } list(int n, const T& val)//構造n個值爲val { CreatHead(); for (int i=0; i < n; i++) { push_back(val); } } template<class iterator> list(iterator first, iterator end)//區間構造 { CreatHead(); while (first != end) { push_back(*first); first++; } } list(const list<T> &L)//拷貝構造 { CreatHead(); Node*cur = L._phead->_pNext; while (cur != L._phead) { push_back(cur->_data); cur = cur->_pNext; } } list <T>& operator=(const list<T> & l)//賦值運算符的重載 { if (&l != this) { clear(); Node*cur = l._phead->_pNext; while (cur != l._phead) { push_back(cur->_data); cur = cur->_pNext; } } return *this; } ~list() { clear(); delete _phead; } ////////////////////////////////////////////////////////////// //迭代器 iterator begin() { return iterator(_phead->_pNext); } iterator end() { return iterator(_phead); } ////////////////////////////////////////////////////////////// //容量 int size() { int size = 0; Node*cur = _phead->_pNext; while (cur != _phead) { size++; cur = cur->_pNext; } return size; } bool empty() { if (_phead->_pNext == _phead) { return true; } return false; } void resize(int newsize, const T&data = T()) { int oldsize = size(); if (newsize > oldsize) { for (int i = oldsize; i < newsize; i++) { push_back(data); } else { for (int i = newsize; i < oldsize; i++) { pop_back(); } } } } /////////////////////////////// // 元素訪問 T& front() { return _pHead->_pNext->_data; } const T& front()const { return _pHead->_pNext->_data; } T& back() { return _pHead->_pPre->_data; } const T& back()const { return _pHead->_pPre->_data; } ////////////////////////////////////////////////////////////// //元素修改 void push_back(const T& data) { insert(end(), data); } void pop_back() { erase(--end()); } iterator insert(iterator pos,const T& data) { Node*cur = new Node(data); Node*tmp = pos._pCur; cur->_pNext = tmp; cur->_pPre = tmp->_pPre; cur->_pPre->_pNext = cur; tmp->_pPre = cur; return iterator(cur); } iterator erase(iterator pos) { Node*del = pos._pCur; if (del == _phead) { return end(); } Node *ret = del->_pNext; del->_pPre->_pNext = del->_pNext; del->_pNext->_pPre = del->_pPre; delete del; return iterator(ret); } void clear()//頭刪 { Node *cur = _phead->_pNext; while (cur != _phead) { _phead->_pNext = cur->_pNext; delete cur; cur = _phead->_pNext; } _phead->_pNext = _phead; _phead->_pPre = _phead; } private: void CreatHead() { _phead = new Node; _phead->_pPre = _phead; _phead->_pNext = _phead; } private: Node* _phead; }; }; int main() { mine::list<int> l(5,10); mine::list<int> s(l); for (auto e : s) { cout << e; } system("pause"); return 0; }