实现一个STL的list容器

实现一个STL的list容器

封装了list的数据结构,和push_back(),push_front(),pop_back(),pop_front(),size()方法,内嵌了iterator迭代器类,还写了for_each()accumulate()模板算法

#include <iostream>
// #include <algorithm>
// #include <list>
#include <exception>
using namespace std;

namespace miniSTL{
template<typename T>
class list{
    struct Node{
        T val;
        Node* prev;
        Node* next;
        Node(const T& val):val(val),prev(NULL),next(NULL){}
    };
    Node* head,*tail;
    size_t _size;
public:
    list():head(NULL),tail(NULL),_size(0){
        head = tail = new Node(T());// 头节点
    }
    // 拷贝构造/赋值运算符重载函数
    ~list(){
        while(NULL != head){
            Node* next = head->next;
            delete head;
            head = next;
        }
        head = tail = NULL;
        _size = 0;
    }
    void push_back(const T& val){
        ++_size;
        Node* node = new Node(val);
        // if(NULL == head){
        //    head = tail = node;
        // }else{
            tail->next = node;
            node->prev = tail;
            tail = node;
        //}
    }

    void push_front(const T& val){
        ++_size;
        Node* node = new Node(val);
        if(head == tail){
            node->prev = head;
            head->next = node;
            tail = node;
        }else{
            node->prev = head;
            node->next = head->next;
            head->next->prev = node;
            head->next = node;
        }
    }
    void pop_back(){
        if(0==_size) return;
        --_size;
        Node* prev = tail->prev;
        prev->next = NULL;
        delete tail;
        tail = prev;
    }
    void pop_front(){
        if(0==_size) return;
        --_size;
        Node* next = head->next;
        head->next = next->next;
        if(NULL!=next->next) next->next->prev = head;
        delete next;
        if(NULL==head->next) tail=head;
    }

    size_t size()const{
        return _size;
    }
    T& operator[](int index){
        // if(index<0 || index >=_size) throw exception(string("invalid index"));
        int count = 0;
        Node* p = head->next;// 头节点的next是下标为0的元素
        while(NULL != p){
            if(count++ == index) break;
            p = p->next;
        }
        return p->val;
    }

    class iterator{
        Node* p;
    public:
        iterator(Node* p):p(p){}
        T& operator*(){return p->val;}
        T* operator->(){return &(p->val);}
        iterator operator++(){
            p=p->next;
            return *this;
        }
        iterator operator++(int){
            iterator tmp(*this);
            p=p->next;
            return tmp;
        }
        iterator operator--(){
            p = p->prev;
            return *this;
        }
        iterator operator--(int){
            iterator tmp(*this);
            p=p->prev;
            return tmp;
        }

        bool operator==(const iterator& it)const{
            return p == it.p;
        }
        bool operator!=(const iterator& it)const{
            return p != it.p;
        }
    };

    iterator begin(){return iterator(head->next);} // 头节点的next是下标为0的元素
    iterator end(){return iterator(NULL);}
};

template <typename IT,typename FUNC>
void for_each(IT first,IT last,FUNC func){
    while(first!=last){
        func(*first++);
    }
}

template <typename IT,typename VAL>
VAL accumulate(IT first,IT last,VAL val){
    while(first!=last){
        val+=*first++;
    }
    return val;
}

}
using namespace miniSTL;

int main(){
    list<int> li;
    li.push_front(1);
    li.push_front(2);
    li.push_front(3);
    //cout << sizeof(li) << endl;

    //cout << li.size() << endl;

    //for(int i=0;i<li.size();++i){
//        cout << li[i] << endl;
    //}

    list<int>::iterator it = li.begin();
    while(it!=li.end()){
        cout << *it++ << " ";
        //++it;
    }
    cout << endl;
    for(int i=0;i<li.size();++i){
        li.pop_front();
        for(auto n:li){
            cout << n << " ";
        }
        cout << endl;
    }




/*
    li.push_front(0);
    li.push_front(-1);
    li.push_front(-2);
    li.pop_back();
    li.pop_back();
    li.pop_back();
    li.pop_back();
    li.pop_back();
    li.pop_back();
    li.push_front(-1);
    li.push_front(-2);
    li.pop_front();
    li.pop_front();
    for_each(li.begin(),li.end(),[](int n){cout << n << endl;});

    cout<< accumulate(li.begin(),li.end(),0) << endl;


    cout << int() << endl;
    cout << float() << endl;
    */
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值