zoukankan      html  css  js  c++  java
  • 栈实现队列

    #include <iostream>
    #include <stack>
    using namespace std;
    
    class MyQueue
    {
    public:
        bool empty();
    
        int front();
    
        void push(int a);
    
        int pop();
    private:
        stack<int> S;
    };
    
    bool MyQueue::empty()
    {
        return S.empty();
    }
    
    int MyQueue::front()
    {
        return S.top();
    }
    
    int MyQueue::pop()
    {
        int i = S.top();
        S.pop();
        return i;    
    }
    
    void MyQueue::push(int a)
    {
        stack<int> temp;
        while(!S.empty())
        {
            temp.push(S.top());
            S.pop();
        }
    
        temp.push(a);
    
        while(!temp.empty())
        {
            S.push(temp.top());
            temp.pop();
        }
    }
    
    int main()
    {
        MyQueue Queue;
        for(int i = 0; i < 5; i++)
        {
            Queue.push(i);
            cout << Queue.front() << endl;
            
        }
        Queue.pop();
        Queue.pop();
        Queue.pop();
        Queue.pop();
        cout << Queue.front() << endl;
        return 0;
    }
  • 相关阅读:
    tiled工具使用
    shan
    随笔
    潘大神又一篇
    潘大神的文章
    最近用到这个强大的工具 PhysicsEditor (转)
    为什么要写博客?
    nyoj 998
    欧拉函数
    背包问题
  • 原文地址:https://www.cnblogs.com/11ys/p/14266083.html
Copyright © 2011-2022 走看看