STL简易list, queue和stack

#include <iostream>
using namespace std;
class Range{
    int B, E, D;
public:
    class Iterator{
    public:
        int index;
        Range& out;
        Iterator(Range& o, int i): out(o), index(i){}
        void operator++(){
            index += out.D;
        }
        int& operator*(){
            return index;
        }
        int operator*()const {
            return index;
        }
        bool operator!=(Iterator i){
            return out.D * (i.index - index) > 0;
        }
    };
    Range(int b = 0, int e = 0, int d = 1): B(b), E(e), D(d){}
    Iterator begin(){
        return Iterator(*this, B);
    }
    Iterator end(){
        return Iterator(*this, E);
    }
};
template <class T = int>
class List{
public:
    struct Node{
        T data;
        Node *next, *prior;
        Node(const T& data): data(data){prior = next = nullptr;}
    };
protected:
    size_t length;
    Node *head, *tail;
public:
    List(){
        length = 0;
        head = tail = nullptr;
    }
    class Iterator{
        Node* node;
        List& list;
    public:
        Iterator(List& list, Node* node): list(list), node(node){};
        void operator++(){node = node->next;}
        T& operator*()const { return  node->data;}
        T operator*(){return node->data;}
        bool operator!=(Iterator i){return node != nullptr;}
    };
    Iterator begin(){return Iterator(*this, head);}
    Iterator end(){return Iterator(*this, nullptr);}
    virtual size_t size(){ return length;}
    virtual bool empty(){return !length;}
    virtual T& front(){ return head->data;}
    virtual T& back(){ return tail->data;}
    virtual void push_back(const T& o){
        Node *p = new Node(o);
        if(length == 0){
            head = tail = p;
        }else{
            p->prior = tail;
            tail->next = p;
            tail = tail->next;
        }
        length++;
    }
    virtual void push_front(const T& o){
        Node *p = new Node(o);
        if(length == 0){
            head = tail = p;
        }else{
            p->next = head;
            head->prior = p;
            head = head->prior;
        }
        length++;
    }
    virtual bool pop_back(){
        if(length == 0) return false;
        else if(length == 1){
            delete head;
            head = tail = nullptr;
            length--;
            return true;
        }else{
            Node* p = tail;
            tail = tail->prior;
            delete p;
            length--;
            return true;
        }
    }
    virtual bool pop_front(){
        if(length == 0) return false;
        else if(length == 1){
            delete head;
            head = tail = nullptr;
            length--;
            return true;
        }else{
            Node* p = head;
            head = head->next;
            delete p;
            length--;
            return true;
        }
    }
};
template <class T = int>
class Stack: protected List<T>{
public:
    Stack(): List<T>(){}
    virtual size_t size(){return this->length;}
    virtual bool empty(){ return !this->length;}
    virtual T& top(){ return this->front();}
    virtual void push(const T& o){this->push_front(o);}
    virtual T& pop(){ this->pop_front(); }
};
template <class T = int>
class Queue: protected List<T>{
public:
    Queue(): List<T>(){}
    virtual size_t size(){return this->length;}
    virtual bool empty(){ return !this->length;}
    virtual T& front(){ return this->head->data;}
    virtual T& back(){ return this->tail->data;}
    virtual void push(const T& o){this->push_back(o);}
    virtual T& pop(){ this->pop_front(); }
};
int main() {
    cout << endl << "---------------range-----------------" << endl;
    for(auto& i: Range(0, 9)) cout << i << " "; cout << endl;
    for(auto i: Range(0, 9, 2)) cout << i << " "; cout << endl;
    for(auto i: Range(9, -1, -1)) cout << i << " "; cout << endl;
    for(auto i: Range(9, -1, -2)) cout << i << " "; cout << endl;

    cout << endl << "---------------list-----------------" << endl;
    List<int> list;
    for(auto i: Range(0, 4))
        list.push_back(i);
    for(auto item: list)
        cout << item << " ";
    cout << endl;
    while(!list.empty())list.pop_back();
    for(auto i: Range(0, 4))
        list.push_front(i);
    for(auto item: list)
        cout << item << " ";
    cout << endl;

    while(!list.empty())list.pop_front();
    for(auto i: Range(0, 4))
        list.push_back(i);
    for(auto item: list)
        cout << item << " ";
    
    cout << endl << "---------------stack-----------------" << endl;
    Stack<int> stack;
    for(auto i: Range(0, 4))stack.push(i);
    cout << stack.size() << endl;
    cout << stack.top() << endl;
    while(!stack.empty()) {
        cout << stack.top() << " ";
        stack.pop();
    }

    cout << endl << "---------------queue-----------------" << endl;
    Queue<double> queue;
    for(auto i: Range(0, 5)) queue.push(i*1.5);
    cout << queue.size() << endl;
    cout << queue.front() << " " << queue.back() << endl;
    while(!queue.empty()){
        cout << queue.front() << " ";
        queue.pop();
    }
    return 0;
}

随便写下,不喜勿喷。

可能有BUG,请自己测试好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值