list模版的模拟实现

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace lee
{
    // List的节点类
	template <class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;
		list_node(const T& val = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _val(val)
		{}
	};
    //List的迭代器类
    //typedef __list_iterator<T, T&, T*> iterator;
    //typedef __list_iterator<T, const T&, const T*> const_iterator;
    template<class T, class Ref, class Ptr> //T模版控制链表类型,Ref(引用),Ptr(指针)控制返回类型
    class __list_iterator
    {
        typedef list_node<T> Node;
        typedef __list_iterator<T, Ref, Ptr> self;
        
    public:
        Node* _node;
        __list_iterator(Node* node)
            :_node(node)
        {}
        //迭代器对象不写析构函数,因为迭代器仅用于访问容器,节点不属于迭代器,不需要迭代器释放。
        Ref operator*()         //可读可写,返回引用
        {
            return _node->_val;
        }
        Ptr operator->()
        {
            return &_node->_val;
        }
        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;
        }
        bool operator!=(const self& it) const
        {
            return _node != it._node;
        }
        bool operator==(const self& it) const
        {
            return _node == it._node;
        }
    };

    //list类
    template<class T>
    class list
    {
        typedef list_node<T> Node;
        //typedef Node* PNode;
    //迭代器设为公有,才可以让外界访问
    public:
        typedef __list_iterator<T, T&, T*> iterator;
        // 这样设计const迭代器是不行的,因为const迭代器期望指向内容不能修改
        // 这样设计是迭代器本身不能修改
        // typedef const __list_iterator<T> const_iterator;
       
        // const T* ptr1;
        // T* const ptr2; 
        typedef __list_iterator<T, const T&, const T*> const_iterator;
    public:
        ///
        // List的构造
        void empty_init()
        {
            _head = new Node;
            _head->_prev = _head;
            _head->_next = _head;
            _size = 0;
        }
        list()
        {
            empty_init();
        }
        list(int n, const T& value = T())
        {
            empty_init();
            for (size_t i = 0; i < n; i++)
            {
                push_back(value);
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            empty_init();
            while (first != last)
            {
                push_back(*first);
                ++first;
            }
        }
        list(const list<T>& lt)
        {
            empty_init();
            for (auto& e : lt)
            {
                push_back(e);
            }
        }
        void swap(list<T>& lt)
        {
            std::swap(_head, lt._head);
            std::swap(_size, lt._size);
        }
        list<T>& operator=(list<T> lt)
        {
            swap(lt);
            return *this;
        }
        ~list()
        {
            clear();
            delete _head;
            _head = nullptr;
        }


        ///
        // List Iterator
        iterator begin()
        {
            return _head->_next;
        }
        iterator end()
        {
            return _head;
        }
        const_iterator begin() const
        {
            return _head->_next;
        }
        const_iterator end() const
        {
            return _head;
        }


        ///
        // List Capacity
        size_t size() 
        {
            return _size;
        }
        bool empty() const
        {
            return _head->_next == _head;
        }


        
        // List Access
        T& front()
        {
            return _head->_next->_val;
        }
        const T& front()const
        {
            return _head->_next->_val;
        }
        T& back()
        {
            return _head->_prev->_val;
        }
        const T& back()const
        {
            return _head->_prev->_val;
        }


        
        // List Modify
        // 在pos位置前插入值为val的节点
        iterator insert(iterator pos, const T& val)
        {
            Node* cur = pos._node;
            Node* prev = cur->_prev;
            Node* newnode = new Node(val);

            prev->_next = newnode;
            newnode->_next = cur;

            cur->_prev = newnode;
            newnode->_prev = prev;

            ++_size;
            return newnode;
        }
        void push_back(const T& val)
        { 
            insert(end(), val); 
        }
        void pop_back() 
        { 
            erase(--end()); 
        }
        void push_front(const T& val) 
        { 
            insert(begin(), val); 
        }
        void pop_front() 
        { 
            erase(begin()); 
        }
        
        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            assert(pos != end());
            Node* cur = pos._node;
            Node* prev = cur->_prev;
            Node* next = cur->_next;

            prev->_next = next;
            next->_prev = prev;
            delete cur;

            --_size;
            return next;
        }
        void clear()
        {
            iterator it = begin();
            while (it != end())
            {
                it = erase(it);
            }
            _size = 0;
        }
      
    private:
        Node* _head;
        size_t _size;
    };
    void Print(const list<int>& lt)
    {
        list<int>::const_iterator it = lt.begin();
        while (it != lt.end())
        {
            // (*it) += 1;
            cout << *it << " ";
            ++it;
        }
        cout << endl;
    }
    void test_list1()
    {
        list<int> lt;
        lt.push_back(1);
        lt.push_back(2);
        lt.push_back(3);
        lt.push_back(4);

        list<int>::iterator it = lt.begin();

        while (it != lt.end())
        {
            (*it) += 1;
            cout << *it << " ";
            ++it;
        }
        cout << endl;

        for (auto e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        Print(lt);
    }

    struct A
    {
        A(int a1 = 0, int a2 = 0)
            :_a1(a1)
            , _a2(a2)
        {}

        int _a1;
        int _a2;
    };

    void test_list2()
    {
        list<A> lt;
        lt.push_back(A(1, 1));
        lt.push_back(A(2, 2));
        lt.push_back(A(3, 3));
        lt.push_back(A(4, 4));

        list<A>::iterator it = lt.begin();
        while (it != lt.end())
        {
            cout << (*it)._a1 << " " << (*it)._a2 << endl;
            cout << it->_a1 << " " << it->_a2 << endl;

            ++it;
        }
        cout << endl;
    }

    void test_list3()
    {
        list<int> lt;
        lt.push_back(1);
        lt.push_back(2);
        lt.push_back(3);
        lt.push_back(4);
        lt.push_front(5);
        lt.push_front(6);
        lt.push_front(7);
        lt.push_front(8);
        for (auto e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        lt.pop_front();
        lt.pop_back();

        for (auto e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        lt.clear();
        lt.push_back(10);
        lt.push_back(20);
        lt.push_back(30);
        lt.push_back(40);
        for (auto e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        cout << lt.size() << endl;
    }

    void test_list4()
    {
        list<int> lt;
        lt.push_back(1);
        lt.push_back(2);
        lt.push_back(3);
        lt.push_back(4);

        for (auto e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        list<int> lt1(lt);
        for (auto e : lt1)
        {
            cout << e << " ";
        }
        cout << endl;

        list<int> lt2;
        lt2.push_back(10);
        lt2.push_back(20);
        lt2.push_back(30);
        lt2.push_back(40);

        for (auto e : lt2)
        {
            cout << e << " ";
        }
        cout << endl;

        lt1 = lt2;

        for (auto e : lt1)
        {
            cout << e << " ";
        }
        cout << endl;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值