<STL>模拟实现List

来源:互联网 发布:开手机淘宝怎么找货源 编辑:程序博客网 时间:2024/06/05 05:01

STL可分为容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adaptors)、算法(algorithms)、仿函数(functors)六个部分。
1.库中list的使用方法

#include <iostream>#include <list>#include <algorithm>#include <stdlib.h>using namespace std;void print(list<int> l){    list<int>::iterator it1 = l.begin();    while (it1 != l.end())    {        cout << *it1 << " ";        it1++;    }    cout << endl;}void ListTest(){    list<int> l;    //push_back();尾插    //push_front();头插    //pop_back();尾删    //pop_front();尾插    l.push_back(1);    l.push_back(2);    l.push_back(3);    l.push_back(4);    l.push_back(5);    //正向迭代器    print(l);    cout << endl;    //反向迭代器    list<int>::reverse_iterator it2 = l.rbegin();    while (it2 != l.rend())    {        cout << *it2 << " ";        it2++;    }    cout << endl;    //有效元素的个数    cout << l.size() << endl;    //所存储的元素个数    cout << l.max_size() << endl;    //判断链表是否为空    cout << l.empty() << endl;    //改变其有效值得个数    //resize(3)   打印出1 2 3     //resize(10)  打印出1 2 3 4 5 0 0 0 0 0     //resize(8,7) 打印出1 2 3 4 5 7 7 7    l.resize(5);    print(l);    cout<<endl;    //第一个数据    cout << l.back() << endl;    //第二个数据    cout << l.front() << endl;    //指定位置前插入数据    list<int>::iterator it3 = l.begin();    it3++;    //l.insert(it3,8);打印结果1 8 2 3 4 5    //l.insert(it3,3,9);//打印结果1 9 9 9 2 3 4 5    print(l);    //删除指定位置数据    list<int>::iterator it4=find(l.begin(),l.end(),4);    l.erase(it4);    print(l);    //交换2个容器数据    list<int>l1;    list<int>l2;    l1.push_back(2);    l2.push_back(3);    l1.swap(l2);    print(l1);    print(l2);}int main(){    ListTest();    system("pause");    return 0;}

2.模拟实现带头结点的双链表

#pragma once#include <iostream>#include <stdlib.h>#include <assert.h>using namespace std;template <class T>struct ListNode{public:    ListNode(const T& x)        :_prev(NULL)        , _next(NULL)        , _data(x)    {}public:    ListNode<T>* _prev;    ListNode<T>* _next;    T _data;};template<class T,class Ref,class Ptr>struct ListIterator{    typedef ListIterator<T, Ref, Ptr> Self;    typedef ListNode<T> Node;public:    ListIterator(Node* node)        :_node(node)    {}    ListIterator()    {}    Ref operator*()    {        return _node->_data;    }    Ptr operator->()    {        return& _node->_data;    }    bool operator==(const Self& s)    {        return _node==s._node;    }    bool operator!=(const Self& s)    {        return _node != s._node;    }    Self& operator++()//后置    {        _node = _node->_next;        return *this;    }    Self& operator++(int)//前置    {        Self tmp(*this);        _node = _node->_next;        return tmp;    }    Self& operator--()//后置    {        _node = _node->_prev;        return *this;    }    Self& operator--(int)//前置    {        Self tmp(*this);        _node = _node->_prev;        return tmp;    }public:    Node* _node;};template<class T>class List{public:    typedef ListNode<T> Node;    typedef ListIterator<T, T&, T*> Iterator;//普通迭代器    typedef ListIterator<T, const T&, const T*> ConstIterator;//const迭代器    Node* BuyNode(const T& x)    {        return new Node(x);    }    List()    {        _head = new Node(T());        _head->_next = _head;        _head->_prev = _head;    }    Iterator End()    {        return _head;    }    Iterator Begin()    {        return _head->_next;    }    ConstIterator End()const    {        return ConstIterator(_head);    }    ConstIterator Begin()const    {        return ConstIterator(_head->_next);    }    template<class InputIterator>    void Insert(Iterator pos, InputIterator first, InputIterator last)    {        while (first != last)        {            Insert(pos, *first);            ++first;        }    }    Iterator Insert(Iterator pos,const T&data )    {        Node* tmp = new Node(data);        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!=_head);        Node* cur = pos._node;        Node* prev = cur->_prev;        Node* next = cur->_next;        prev->_next = next;        next->_prev = prev;        delete cur;        return prev;    }    void PushBack(const T& x)    {        Insert(End(),x);    }    void PushFront(const T& x)    {        Itsert(Begin(),x)    }    void PopBack()    {        assert(_head!=_head->_next);        Erase(End());    }    void PopFront()    {        assert(_head != _head->_next);        Erase(Begin());    }protected:    Node* _head;};void PrintList1(const List<int>& l){    List<int>::ConstIterator it = l.Begin();    while (it!=l.End())    {        cout << *it << " ";        it++;    }    cout << endl;}void PrintList2(List<int>& l){    List<int>::Iterator it = l.Begin();    while (it != l.End())    {        cout << *it << " ";        it++;    }    cout << endl;}int main(){    List<int> l;    l.PushBack(1);    l.PushBack(2);    l.PushBack(3);    l.PushBack(4);    List<int>::Iterator pos = l.Begin();    pos++;    l.Insert(pos,9);    PrintList1(l);    pos++;    l.Erase(pos);    PrintList1(l);    system("pause");    return 0;}
0 0