剑指offer第五题:用两个栈实现队列(c++实现)

19 篇文章 0 订阅

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:
入栈时将元素入stack1,同时判断stack2是否为空,空则将stack1中元素顺序入栈到stack2中;
出栈时1)先判断stack2是否为空,空则判断stack1是否为空,stack1不空则将其元素全部入栈到stack2中;2)获取stack2栈顶元素并对stack2执行出栈操作.

#include <iostream> 
#include <vector> 
#include <stack>
#include <algorithm>  
using namespace std; 

class myList
{
private:
   stack<int> stack1;//入
   stack<int> stack2;//出
public:
    myList(/* args */);
    ~myList();
    //入队列
    void push(int node) {
        stack1.push(node);//如stack1
        if(stack2.empty())//判断是否需要将stack1中元素放到stack2中
        {
            while(!stack1.empty())//将stack1中元素全部放入stack2中
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }        
    }
    int pop() {//出队列
        if(stack2.empty())//stack为空
        {
            while (!stack1.empty())//stack1非空时将其元素全部放入stack2
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        int result=stack2.top();//获取stack2栈顶元素
        stack2.pop(); //stack2出栈
        return result;       
    }
    //获取队列列首元素
    int top(){
        if(stack2.empty())//stack2为空
        {
            if(!stack1.empty())//stack1非空,将其元素全部入栈stack2
            {
                while (!stack1.empty())
                {
                    stack2.push(stack1.top());
                    stack1.pop();
                }
            }
            else//stack1空,返回非正常值
            {
                return -1;
            }                    
        }
        return stack2.top();//返回stack2栈顶元素
    }
    //判断队列是否为空
    bool isEmpty(){
        if(stack1.empty()&&stack2.empty())//stack1和stack2均为空则空
        {
            return true;
        }
        else//否则非空
        {
            return false;
        }
        
    }
    //获取队列容量
    int size(){
        return stack1.size()+stack2.size();
    }
};

myList::myList(/* args */)
{
}

myList::~myList()
{
}
//打印出队列元素
void printPop(myList* lst)
{
    int tmp=lst->pop();
    if(tmp==-1)
        cout<<"list is empty!"<<endl;
    else
        cout<<tmp<<endl;
}
//打印队首元素
void printTop(myList* lst)
{
    int tmp=lst->top();
    if(tmp==-1)
        cout<<"list is empty!"<<endl;
    else
        cout<<tmp<<endl;
}

int main() 
{  
    myList *tstLst = new myList();
    for(int i=0;i<5;i++)
    {
        tstLst->push(i*2);
    }

    printTop(tstLst);

    printPop(tstLst);
        
    printTop(tstLst);

    tstLst->push(9);
    printPop(tstLst);
    printPop(tstLst);
    printPop(tstLst);
    printPop(tstLst);
    printPop(tstLst);
    
    printTop(tstLst);

    tstLst->push(3);
    tstLst->push(4);
    printTop(tstLst);
    printPop(tstLst);
    printTop(tstLst);

    for(int i=0;i<5;i++)
    {
        tstLst->push(i*3);
    }
    
    printPop(tstLst);
    
    return 0; 
}

具体思路可参考以下文章:

剑指Offer面试题:6.用两个栈实现队列

《剑指offer》——用两个栈实现队列

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值