一个简单的List实现

list和vector是STL库中两个最基本的容器,以下代码模拟实现一个简单的list:

#ifndef _MYLIST_
#define _MYLIST_

#include<iostream>
using namespace std;

class ListIsEmpty{};
class ArgIsIll{};

template<typename T>
class Node{
public:
    Node(const T& d=T()):data(d),prev(NULL),next(NULL)
    {}
    ~Node()
    {}
private:
    Node(const Node& n);
    Node& operator=(const Node& n);
public:
    T data;
    Node* prev;
    Node* next;
};

template<typename T>
class List{
public:
    List():front(NULL),tail(NULL),cur_size(0)
    {}
    ~List()
    {
        while(!this->empty())
        {
            this->pop_front();
        }
    }
    List& operator=(const List& l)
    {
        if(&l!=this)
        {
            Node<T>* cur=l.front;
            List newlist;
            while(cur!=NULL)
            {
                newlist.push_front(cur->data);
            }
        }
        return newlist;
    }
    Node<T>* begin()
    {
        return front;
    }
    Node<T>* end()//此处end函数与STL中稍微有点不同,STL中end返回值指向最后一个元素的下一个,这里指向最后一个元素
    {
        return tail;
    }
    bool empty()
    {
        if(cur_size==0)
            return true;
        else
            return false;
    }
    size_t size()
    {
        return cur_size;
    }
    bool push_front(Node<T>* n)
    {
        if(this->empty())
        {
            front=tail=n;
        }
        else
        {
            n->next=front;
            front->prev=n;
            n->prev=tail;
            tail->next=n;
            front=n;
        }
        cur_size++;
        return true;
    }
    bool pop_front()throw (ListIsEmpty)
    {
        if(this->empty())
        {
            throw ListIsEmpty();
        }
        else if(this->size()==1)
        {
            delete front;
            front=tail=NULL;
        }
        else
        {
            Node<T>* s=front;
            front=front->next;
            front->prev=tail;
            tail->next=front;
            delete s;
        }
        cur_size--;
        return true;
    }
    bool push_front(const T& t)
    {
        Node<T> *s=new Node<T>(t);
        return push_front(s);
    }
    bool push_back(Node<T>* n)
    {
        if(this->empty())
        {
            front=tail=n;
        }
        else
        {
            tail->next=n;
            n->prev=tail;
            n->next=front;
            front->prev=n;
            tail=n;
        }
        cur_size++;
        return true;
    }
    bool pop_back()throw (ListIsEmpty)
    {
        if(this->empty())
        {
            throw ListIsEmpty();
        }
        else if(this->size()==1)
        {
            delete tail;
            front=tail=NULL;
        }
        else
        {
            Node<T>* s=tail;
            tail=tail->prev;
            front->prev=tail;
            tail->next=front;
            delete s;
        }
        cur_size--;
        return true;
    }
    bool push_back(const T& t)
    {
        Node<T> *s=new Node<T>(t);
        return push_back(s);
    }
    bool insert(size_t pos,Node<T>* n)throw (ArgIsIll)
    {
        if(pos>this->size())
        {
            throw ArgIsIll();
        }
        else if(pos==this->size())
        {
            return push_back(n);
        }
        else if(pos==0)
        {
            return push_front(n);
        }
        else
        {
            size_t i=0;
            Node<T>* cur=front;
            for(i=0;i<pos-1;++i)
            {
                cur=cur->next;
            }
            n->next=cur->next;
            cur->next->prev=n;
            n->prev=cur;
            cur->next=n;
            cur_size++;
            return true;
        }
    }
    bool insert(size_t pos,const T& t)throw (ArgIsIll)
    {
        Node<T>* s=new Node<T>(t);
        return insert(pos,s);
    }
    bool erase(size_t pos)
    {
        if(pos>this->size())
        {
            throw ArgIsIll();
        }
        else if(pos==this->size())
        {
            return pop_back();
        }
        else if(pos==0)
        {
            return pop_front();
        }
        else
        {
            size_t i=0;
            Node<T>* cur=front;
            for(i=0;i<pos-1;++i)
            {
                cur=cur->next;
            }
            cur->next->prev=cur->prev;
            cur->prev->next=cur->next;
            delete cur;
            cur_size--;
            return true;
        }
    }
    void print()const
    {
        Node<T>* cur=front;
        while(true)
        {
            cout<<cur->data<<" ";
            if(cur==tail)
                break;
            cur=cur->next;
        }
        cout<<endl;
    }
private:
    List(const List& l);
private:
    Node<T>* front;
    Node<T>* tail;
    size_t cur_size;
};

#endif

以上

如果你有任何想法或是可以改进的地方,欢迎和我交流!

完整代码及测试用例在github上:点我前往

本文首发于www.sbrave.cn

【完】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值