【LeetCode & 剑指offer刷题】栈与队列题1:9.1 用队列实现栈(225. Implement Stack using Queues)...

【LeetCode & 剑指offer刷题】栈与队列题1:9.1 用队列实现栈(225. Implement Stack using Queues)

【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

225. Implement Stack using Queues

Implement the following operations of a stack using queues.
  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use  only  standard operations of a queue -- which means only  push to back peek/pop from front size , and  is empty  operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
 
//问题:用队列实现栈
/*
方法一:用两个队列
 插入 q1借助辅助队q2在队首插入元素
 删除 q1队首元素出队
 访问 访问q1队首元素
 判断 判断q1是否为空
(Two Queues, push - O(n), pop O(1)
*/
#include <queue>
class MyStack
{
private :
    queue < int > q1 , q2 ;
public :
    /** Initialize your data structure here. */
    MyStack ()
    {
       
    }
   
    /** Push element x onto stack. */
    void push ( int x )
    {
        while (! q1 . empty ()) //q1转移到q2
        {
            q2 . push ( q1 . front ());
            q1 . pop ();
        }
        q1 . push ( x ); //将元素插入到q1中(处于队首位置)
        while (! q2 . empty ()) //q2转移回q1
        {
            q1 . push ( q2 . front ());
            q2 . pop ();
        }
    }
   
    /** Removes the element on top of the stack and returns that element. */
    int pop ()
    {
        int top_element = q1 . front ();
        q1 . pop ();
        return top_element ;
    }
   
    /** Get the top element. */
    int top ()
    {
        return q1 . front ();
    }
   
    /** Returns whether the stack is empty. */
    bool empty ()
    {
        return q1 . empty ();
    }
};
 
/*
方法二:用两个队列
 插入 在queue1中插入
 删除 quue1出队,queue2入队,将queue1中除队尾元素外全部转移到queue2中,再删除queue1中元素即可,最后再q2中元素复制过去q1 (主队q1借助辅助队q2将队尾元素删除)
 访问 访问queue1队尾元素
 判断是否为空 判断queue1是否为空
 
注意:stack只有top,某次只能访问栈顶元素,而queue有front,back,某次可以访问队首和队尾元素,不过只能删除队首元素
Two Queues, push O(1), pop O(n)
*/
#include <queue>
class MyStack
{
private :
    queue < int > q1 , q2 ;
public :
    /** Initialize your data structure here. */
    MyStack ()
    {
       
    }
   
    /** Push element x onto stack. */
    void push ( int x )
    {
        q1 . push ( x );
    }
   
    /** Removes the element on top of the stack and returns that element. */
    int pop ()
    {
        int top_element = top ();
        while ( q1.size() > 1 ) //为删除队尾元素,将q1元素除队尾元素外转移到q2
        {
            q2 . push ( q1 . front ());
            q1 . pop (); //删除第一个元素
        }
        q1 . pop (); //删除队尾元素
        while (! q2 . empty ()) //将元素再转移到q1
        {
            q1 . push ( q2 . front ());
            q2 . pop ();
        }
        return top_element ;
    }
   
    /** Get the top element. */
    int top ()
    {
        return q1.back(); //返回队尾元素即为栈顶元素
    }
   
    /** Returns whether the stack is empty. */
    bool empty ()
    {
        return q1 . empty () ;
    }
};
/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */
//方法三:一个队列
//每次push某值后,将队首元素依次push到后面并pop队首元素,直到该值称为队首新的队首元素
/*
class Stack {
public:
    queue<int> que;
    // Push element x onto stack.
    void push(int x) {
        que.push(x);
        for(int i=0;i<que.size()-1;++i){
            que.push(que.front());
            que.pop();
        }
    }
    // Removes the element on top of the stack.
    void pop() {
        que.pop();
    }
    // Get the top element.
    int top() {
        return que.front();
    }
    // Return whether the stack is empty.
    bool empty() {
        return que.empty();
    }
};
*/
 
 

 

posted @ 2019-01-05 16:39 wikiwen 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值