温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

模拟实现stl中的list

发布时间:2020-06-20 06:25:14 来源:网络 阅读:324 作者:be_better_ 栏目:编程语言

list中的接口有sort(默认是按照由小到大的顺序排,可以更改排序方式,也可排完后调用reverse()逆置)
模拟实现list即将list中的函数模拟实现,同样也分为五部分:构造与析构、容量、迭代器、元素访问、元素修改。
需要注意的问题是list中的迭代器,与vector中不同的是list不是顺序结构,所以我们要对迭代器进行封装,其使用规则也要在这个封装中自定义给出。vector中的使用原生态指针即可。
代码如下:

#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;
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI