栈、队列、堆一些题目和算法实现

// 栈、队列、堆一些题目和算法实现

/***************************************************
*  函数功能: 使用队列实现栈的push,pop,top,empty操作
*  参数说明
*
*  日期:2018-06-10-09.31
***************************************************/
class MyStack
{
public:
    MyStack(){}
    void push1(int x)
    {
        int len=q1.size();
        q1.push(x);
        while(len--)
        {
            q1.push(q1.front());
            q1.pop();
        }
    }
    void push(int x)
    {
        std::queue<int> q2;
        q2.push(x);
        while(!q1.empty())
        {
            q2.push(q1.front());
            q1.pop();
        }
        while(!q2.empty())
        {
            q1.push(q2.front());
            q2.pop();
        }

    }
    int pop()
    {
        int x=q1.front();
        q1.pop();
        return x;

    }
    int top()
    {
        if(!q1.empty())
        {
            return q1.front();
        }
        return -1;
    }
    bool empty()
    {
        return q1.empty();
    }
private:
    std::queue<int> q1;

};

/***************************************************
*  函数功能:使用2个栈实现一个队列,实现push,pop,peek,empty功能,时间复杂度0(1)
*  参数说明
*
*  日期:2018-06-10-11.07
***************************************************/
class MyQueue
{
public:
    MyQueue(){}
    void push(int x)
    {
        if(s2.empty())
        {
            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }
        s1.push(x);
    }
    int pop()
    {
        if(s2.empty())
        {
            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }
        int x=s2.top();
        s2.pop();
        return x;
    }
    int peek() //相对于front
    {
        if(s2.empty())
        {
            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }
        return s2.top();
    }
    bool empty()
    {
        return (s1.empty() && s2.empty());
    }
private:
    std::stack<int> s1;
    std::stack<int> s2;
};
/***************************************************
*  函数功能:使用1个栈实现一个队列,实现push,pop,peek,empty功能,时间复杂度0(1)
*  参数说明
*
*  日期:2018-06-10-11.07
***************************************************/
class MyQueue1
{
public:
    MyQueue1(){}
    void push(int x)
    {
        if(s.empty())
        {
            fronts=x;
        }
        s.push(x);

    }
    int pop()
    {
        std::stack<int> temp;
        int len=s.size();
        while(--len)
        {
            temp.push(s.top());
            s.pop();
        }
        int ret=s.top();
        s.pop();
        if(!temp.empty()) fronts=temp.top();

        while(!temp.empty())
        {
            s.push(temp.top());
            temp.pop();
        }
        return ret;
    }
    int peek()
    {
        return fronts;
    }
    bool empty()
    {
        return s.empty();
    }
private:
    std::stack<int> s;
    int fronts;
};
/***************************************************
*  函数功能: 最小栈,实现push,pop,top,getMin功能
*  时间复杂度:0( 1) ; 空间复杂度:0( )
*  题目来源:https://leetcode.com/problems/min-stack/description/
*  参数说明
*
*  日期:2018-06-10-11.23
***************************************************/
class MinStack{
public:
    MinStack(){}
    void push(int x)//压栈
    {
        s.push(x);
        if(minS.empty() || minS.top()>=x)
        {
            minS.push(x);
        }

    }
    void pop() //弹栈
    {
        if(s.top()==minS.top())
        {
            minS.pop();
        }
        s.pop();

    }
    int top() //返回栈顶元素
    {
        return s.top();
    }
    int getMin() // 返回栈内最小元素
    {
        return minS.top();
    }

private:
    std::stack<int> s;
    std::stack<int> minS;
};


猜你喜欢

转载自blog.csdn.net/csu_guo_liang/article/details/80784786