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

本文介绍如何使用队列实现栈的功能,并探讨利用两个栈实现队列的方法,同时展示仅用一个栈实现队列的可能性。此外,还提供了一个最小栈的实现方案,能够高效获取栈中最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/***************************************************
*  函数功能: 使用队列实现栈的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;
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值