可遍历队列

昨天可能是太晚了,晕了

改进了一下:

struct A
{
    int x;
    A(int x):x(x){}
    void setX(int x){this->x = x;}
    int getX(){return x;}
    void print(){
        if(this == nullptr){
            // cout<<"print error: this is NULL"<<endl;
            cout<<"this is NULL"<<endl;
            return;
        }
        // cout<<this<<endl;
        cout<<"value is: "<<x<<endl<<endl;
    }
};

//不算容器
//目前这样用着更爽
template <typename T> class MyQueue
{
public:
struct item{
    T *x;
    item *next;

    item(T *X):
        x(X),
        next(nullptr)
        {
    }
    ~item()  {
        delete x;
    }
};

private:
    item *head;
    item *tail;
    item *now;
public:
    MyQueue():
        head(nullptr),
        tail(nullptr),
        now(nullptr)
    {};
    ~MyQueue(){
        clear();
    };
    void push(T *X)   {
        if(tail != nullptr) tail->next = new item(X), tail = tail->next;
        else tail = head = now = new item(X);
    };
    void pop()  {
        if(head == nullptr){
            // cout<<"pop error: it's empty now"<<endl;
            tail = now = nullptr;
            return;
        }
        item *tmp = head;
        if(now == head) now = head->next;
        head = head->next;
        
        //???
        delete tmp;
    };
    T* front()  {
        if(head == nullptr){
            // cout<<"front error: it's empty now"<<endl;
            return nullptr;
        }
        return head->x;
    }
    T* back()   {
        if(tail == nullptr){
            // cout<<"back error: it's empty now"<<endl;
            return nullptr;
        }
        return tail->x;
    }
    T* nextone(){
        if(now == nullptr){
            // cout<<"nextone error: over"<<endl;
            return nullptr;
        }
        item *tmp = now;
        now = now->next;
        return tmp->x;
    }
    
    bool isSafe(){
        return !(now == nullptr);
    }
    
    void reset(){
        now = head;
    }

    void clear(){
        while(head != nullptr){
            item *tmp = head;
            head = head->next;
            tmp->~item();
        }
        head = tail = nullptr;
    };
};

int main(){
    MyQueue<A> tmp;
    
    for(int i = 1; i <= 3; i++) tmp.push(new A(i));
    
    tmp.front()->print();
    tmp.back()->print();
    tmp.pop();
    tmp.front()->print();    
    
    A *tmpA = new A(0);
    tmpA = tmp.nextone();
    cnm(1);
    tmpA->print();

    tmpA = tmp.nextone();
    cnm(2);
    tmpA->print();
    
    tmpA = tmp.nextone();
    cnm(3);
    tmpA->print();

    tmpA = tmp.nextone();
    cnm(4);
    tmpA->print();

    cnm(5);
    tmp.pop();
    tmp.pop();
    tmp.pop();

    // cout<<"front: "<<tmp.front()<<"  back: "<<tmp.back()<<endl;
    tmp.front()->print();
    tmp.back()->print();

    // for(int i = 1; i <= 10; i++)
    //     tmp.push(new A(i));
    
    // while(tmp.isSafe())
    //     tmp.nextone()->print();

    // tmp.reset();

    // while(tmp.isSafe())
    //     tmp.nextone()->print();

    return 0;
}

输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值