1. 程式人生 > >模擬實現STL (雙)連結串列

模擬實現STL (雙)連結串列

#include<iostream>
using namespace std;
#include<assert.h>
#include<list>
#include<vector>
template<class T>
struct  _ListNode
{
    T _data;
    _ListNode<T>* _next;
    _ListNode<T>* _prev;
    _ListNode(const T& x)
        :_data(x)
        ,_next(NULL)
        , _prev(NULL)
    {}
};
template
<class T,class Ref,class Ptr> struct _ListIterator { typedef _ListNode<T> Node; typedef _ListIterator<T, Ref, Ptr> Self; _ListIterator(Node *node) :_node(node) {} Ref operator*() { return _node->_data; } bool operator==(const Self& s) const
{ return this->_node == s._node; } Self& operator++() { _node = _node->_next; return *this; } Self& operator++(int) { Node* cur = _node; _node = _node->_next; return *cur; } Self& operator--() { _node = _node->_prev; return
*this; } Self& operator--(int) { Node* cur = _node; _node = _node->_prev; return *cur; } // it != l.End() // it.operator != (&it, l.End()) bool operator != (const Self& s) const { return this->_node != s._node; } Node* _node; }; // 迭代器 template<class T> class List { typedef _ListNode<T> Node; public: typedef _ListIterator<T, T&, T*> Iterator; typedef _ListIterator<T, const T&, const T*> 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); } ConstIterator Begin() const { return ConstIterator(_head->_next); } Iterator End() { return Iterator(_head); } 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; } void PopBack() { if (_head->_next != _head) { Node* tail = _head->_prev; Node* prev = tail->_prev; delete tail; prev->_next = _head; _head->_prev = prev; } } void PopFront() { if (_head->_next != _head) { Node *prev = _head->_next; Node *cur = prev->_next; delete prev; _head->_next = cur; cur->_prev = _head; } } void PushFront(const T& x) { Node *tmp = GetNode(x); Node *cur = _head->_next; tmp->_next = cur; tmp->_prev = _head; _head->_next = tmp; cur->_prev = tmp; } Iterator Find(const T& x) { if (_head->_next == _head) return NULL; else { Node *cur=_head->_next; while (cur != _head) { if (cur->_data == x) { return cur; } cur = cur->_next; } return NULL; } } void Insert(Iterator pos,const T &x) // 指定元素前插入資料 { assert(pos._node); Node *tmp =GetNode(x); Node *cur = _head->_next; while (cur->_next != pos._node) { cur = cur->_next; } Node* prev = cur->_next; tmp->_next = prev; tmp->_prev = cur; cur->_next = tmp; prev->_prev = tmp; } Iterator Erase(Iterator pos) { assert(pos._node); Node* cur = _head; while (cur->_next != pos._node) { cur = cur->_next; } cur->_next = pos._node->_next; pos._node->_next->_prev = cur; delete pos._node; pos._node = NULL; return pos._node; } protected: Node* _head; }; void PrintMyList(const List<int>& l1) { // [) List<int>::ConstIterator it = l1.Begin(); while (it != l1.End()) { //*it = 10; cout << *it << " "; ++it; } cout << endl; } void TestMyList() //執行結果:0 1 2 3 { List<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.PushBack(5); l1.PushFront(0); l1.PopBack(); l1.PopFront(); l1.Insert(l1.Find(1), 0); l1.Erase(l1.Find(4)); PrintMyList(l1); } void PrintVector(const vector<int>& v) { // 普通迭代器 -- 可讀可寫 // const 迭代器 -- 可讀 // 反向迭代器 -- 反著遍歷 // [begin, end) vector<int>::const_iterator it = v.begin(); while (it != v.end()) { /* if (*it % 2) { cout<<*it<<" "; }*/ //(*it)++; cout << *it << " "; ++it; } cout << endl; vector<int>::const_reverse_iterator rIt = v.rbegin();//reverse while (rIt != v.rend()) { cout << *rIt << " "; ++rIt; } cout << endl; } void TestVector() { vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); PrintVector(v1); vector<int> v2(3, 10); // 型別--類似指標的一個物件 -- 智慧指標 PrintVector(v2); } void PrintList(list<int>& l) { list<int>::const_iterator it = l.begin(); while (it != l.end()) { cout << *it << " "; //*it = 10;//const迭代器,不可修改 ++it; } cout << endl; } void TestList() { list<int> l1; l1.push_back(1); l1.push_back(2); l1.push_back(3); l1.push_back(4); PrintList(l1); list<int> l2(5, 1); PrintList(l2); } int main() { //TestList(); TestMyList(); system("pause"); return 0; }