栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现。

一.用两个栈实现队列

我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B)。

1.入队列:

把元素放进栈A即可。假如栈A已满并且栈B为空,可以先把栈A中的所有元素先弹出并放入栈B中;假如栈B不为空,则出错了(不能插入)。

2.出队列:

假如栈B不为空,直接弹出。假如栈B为空,由于队列是先进先出的,因此要出队列时,我们要先把栈A中的元素全部放进栈B中,然后再从栈B中弹出栈顶元素。

3.例子:

进行以下操作:

(1)插入1:

栈A:1

栈B:空

(2)插入2(左边为栈顶):

栈A:2 1

栈B:空

(3)出队列:

栈B为空,先把栈A的元素弹出插入栈B:

栈A:空
栈B:1 2

栈B弹出:
栈A:空

栈B:2

(4)出队列

栈B不为空,直接弹出:
栈A:空

栈B:空

这样,进队列的顺序为1  2,出队列的顺序为1 2,满足队列的特性。

4.LeetCode相关题目

232. Implement Queue using Stacks(https://leetcode.com/problems/implement-queue-using-stacks/description/):

这道题的考虑的东西很少,没考虑一些特殊情况:

#include <iostream>
#include <stack>
using namespace std;
class MyQueue {
public:
stack<int> in;
stack<int> out;
/** Initialize your data structure here. */
MyQueue() {
} /** Push element x to the back of queue. */
void push(int x) {
in.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
if (!out.empty()) {
int temp = out.top();
out.pop();
return temp;
}
else {
if (in.empty()) {
return -;
}
else {
while (!in.empty()) {
out.push(in.top());
in.pop();
}
int temp = out.top();
out.pop();
return temp;
}
}
} /** Get the front element. */
int peek() {
if (!out.empty()) {
return out.top();
}
else {
if (in.empty()) {
return -;
}
else {
while (!in.empty()) {
out.push(in.top());
in.pop();
}
return out.top();
}
}
} /** Returns whether the queue is empty. */
bool empty() {
return in.empty() && out.empty();
}
}; /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/

二.用两个队列实现栈:

1.用队列实现栈有两种方法,两种方法的入栈和出栈的时间复杂度不相同,按需使用:

假设有两个队列,一个队列A,不为空,一个队列B,为空。队列A中的元素符合栈操作的前提。
(1)入栈O(n),出栈(1)

入栈时,直接把元素放进空的队列B,然后把队列A所有的元素按顺序放到队列B中。队列A就变为空了。此时,最后一个“入栈”的元素就成了队列头,需要弹出直接从队列B中弹出即可。这样就满足了栈后进先出的特性了。之后的操作两个队列交替就行了。

(2)入栈O(1),出栈(n)

入栈时,直接把元素放进不为空的队列A的队尾;出栈时,把栈A的前n-1个元素放入栈B中,栈A中剩下一个的元素就是最新插入的元素,直接出队列,也满足栈的特性了。

2.LeetCode相关题目

225. Implement Stack using Queues(https://leetcode.com/problems/implement-stack-using-queues/description/)

这道题用第(1)种方法会快一点,说明出栈操作多一点吧。

方法(1):

class MyStack {
public:
/** Initialize your data structure here. */
queue<int> a;
queue<int> b;
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if (a.empty() && b.empty()) {
a.push(x);
return;
}
if (a.empty()) {
a.push(x);
while (!b.empty()) {
a.push(b.front());
b.pop();
}
}
else {
b.push(x);
while (!a.empty()) {
b.push(a.front());
a.pop();
}
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
if (a.empty()) {
int temp = b.front();
b.pop();
return temp;
}
else {
int temp = a.front();
a.pop();
return temp;
}
} /** Get the top element. */
int top() {
return a.empty() ? b.front() : a.front();
} /** Returns whether the stack is empty. */
bool empty() {
return a.empty() && b.empty();
}
};

方法(2):

class MyStack {
public:
/** Initialize your data structure here. */
queue<int> a;
queue<int> b;
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if (a.empty() && b.empty()) {
a.push(x);
return;
}
if (!a.empty()) {
a.push(x);
}
if (!b.empty()) {
b.push(x);
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
if (!a.empty()) {
while (a.size() != ) {
b.push(a.front());
a.pop();
}
int temp = a.front();
a.pop();
return temp;
}
else {
while (b.size() != ) {
a.push(b.front());
b.pop();
}
int temp = b.front();
b.pop();
return temp;
}
} /** Get the top element. */
int top() {
if (!a.empty()) {
while (a.size() != ) {
b.push(a.front());
a.pop();
}
int temp = a.front();
b.push(a.front());
a.pop();
return temp;
}
else {
while (b.size() != ) {
a.push(b.front());
b.pop();
}
int temp = b.front();
a.push(b.front());
b.pop();
return temp;
}
} /** Returns whether the stack is empty. */
bool empty() {
return a.empty() && b.empty();
}
};

栈和队列数据结构的相互实现[LeetCode]的更多相关文章

  1. 栈和队列数据结构的基本概念及其相关的Python实现

    先来回顾一下栈和队列的基本概念: 相同点:从"数据结构"的角度看,它们都是线性结构,即数据元素之间的关系相同. 不同点:栈(Stack)是限定只能在表的一端进行插入和删除操作的线性 ...

  2. 九度OJ 1512 用两个栈实现队列 【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1512 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 输入: 每 ...

  3. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  4. 数据结构和算法(Golang实现)(14)常见数据结构-栈和队列

    栈和队列 一.栈 Stack 和队列 Queue 我们日常生活中,都需要将物品排列,或者安排事情的先后顺序.更通俗地讲,我们买东西时,人太多的情况下,我们要排队,排队也有先后顺序,有些人早了点来,排完 ...

  5. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  6. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  7. 数据结构之栈和队列及其Java实现

    栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...

  8. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  9. 详细分析栈和队列的数据结构的实现过程(Java 实现)

    目录 栈和队列的数据结构的实现过程(Java 实现) 栈的数据结构的实现 栈的基础知识回顾 栈的常见应用 基于数组的栈的实现 具体代码设计 基于数组的栈简单的时间复杂度分析 关于栈的一个算法应用:括号 ...

随机推荐

  1. 这些工具对html5开发有很大帮助

    如今H5已经在IT这块很热门,所以也就有越来越多的人自学或是报名培训班学习H5,今天写一篇关于当下html5开发工具有哪些?哪个更好一些? 浅谈2017年html5开发工具哪个好: 1.Adobe D ...

  2. 安卓自定义控件(四)实现自定义Layout

    本来我是不准备写这篇文章的,我实在想不出有什么样奇怪的理由,会去继承ViewGroup然后自定义一个布局,大概是我的项目经验还不够吧,想了好久,想到了这样一个需求: 需求 如图:在项目中经常有一个这样 ...

  3. 数字三角形-poj

    题目要求: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 在上面的数字三角形中寻找在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左 ...

  4. javascript的BOM,DOM对象

    BOM对象 window对象 所有浏览器都支持 window 对象.概念上讲.一个html文档对应一个window对象.功能上讲: 控制浏览器窗口的.使用上讲: window对象不需要创建对象,直接使 ...

  5. Centos7.0 下挂载磁盘

    参考:

  6. sql server 2008 r2 登陆时显示无法打开默认的数据库

    解决! 第一步: 远程其他服务器的数据库能连上,本地的数据库某个用户名就是打不开,一开始以为是用户名或者密码错误, 后来用sqlcmd dos命令 -S . -U an -P sa 的方式登陆时可以的 ...

  7. Nginx日志切割案例讲解,Nginx的知识讲解

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如在产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将会导致日志文件容量 ...

  8. PHP生成 uuid

    // 生成UUID,并去掉分割符 function guid() { if (function_exists('com_create_guid')){ $uuid = com_create_guid( ...

  9. Android自定义processor实现bindView功能

    一.简介 在现阶段的Android开发中,注解越来越流行起来,比如ButterKnife,Retrofit,Dragger,EventBus等等都选择使用注解来配置.按照处理时期,注解又分为两种类型, ...

  10. 为PowerApps和Flow,Power BI开发自定义连接器

    作者:陈希章 发表于 2017年12月20日 前言 我在之前用了几篇文章来介绍新一代微软商业应用平台三剑客(PowerApps,Microsoft Flow,Power BI),相信对于大家会有一种跃 ...