如何使用两个堆栈实现队列

Stack and Queue at a glance...

堆叠和排队一目了然...

Stack:

堆栈:

The stack is an ordered list where insertion and deletion are done from the same end, top. The last element that entered first is the first one to be deleted (the basic principle behind the LIFO). That means it is a data structure which is implemented as LIFO.

堆栈是一个有序列表,其中插入和删除从同一末端开始。 首先输入的最后一个元素是要删除的第一个元素(LIFO的基本原理)。 这意味着它是一个实现为LIFO的数据结构。

The main stack operations are (basic ADT operations)...

主堆栈操作为(基本ADT操作)...

  1. push (int data): Insertion at top

    push(int数据):在顶部插入

  2. int pop(): Deletion from top

    int pop():从顶部删除

Queue:

队列:

The queue is an ordered list in which insertions are done at one end (rear) and deletions are done from another end (front). The first element that got inserted is the first one to be deleted (basic principle of FIFO). That means it is a data structure which is implemented as FIFO.

队列是一个有序列表,其中插入是在一端(后部)完成,而删除是从另一端(前部)完成。 插入的第一个元素是要删除的第一个元素(FIFO的基本原理)。 这意味着它是一个实现为FIFO的数据结构。

The main Queue operations are (basic ADT operations)...

主要的Queue操作是(基本ADT操作)...

  1. EnQueue (int data): Insertion at rear end

    EnQueue(int数据):在后端插入

  2. int DeQueue(): Deletion from front end

    int DeQueue():从前端删除

使用两个队列实现堆栈 (Implementation of a stack using two queues)

Likewise, a queue can be implemented with two stacks, a stack can also be implemented using two queues. The basic idea is to perform stack ADT operations using the two queues.

同样,一个队列可以用两个堆栈实现,一个堆栈也可以用两个队列实现。 基本思想是使用两个队列执行堆栈ADT操作。

So, we need to implement push(),pop() using DeQueue(), EnQueue() operations available for the queues.

因此,我们需要使用队列可用的DeQueue()和EnQueue()操作来实现push(),pop()。

Implementation:

实现方式:

Let q1 and q2 be the two queues...

设q1和q2为两个队列...

struct stack{struct queue *q1;
struct queue *q2;
}

Algorithm to implement push and pop:

实现推送和弹出的算法:

Push operation algorithm:

推送操作算法:

  1. Check whether q1 is empty or not. If q1 is empty then EnQueue the element to q2.

    检查q1是否为空。 如果q1为空,则将元素排队到q2。

  2. Otherwise EnQueue to q1.

    否则,排队到q1。

push(struct stack *s,int data){if(isempty(s->q1))
EnQueue(s->q2,data);
else
EnQueue(s->q1,data);
}

Pop operation algorithm

弹出操作算法

The basic idea is to transfer n-1 elements (let n be the total no of elements) to other queue and delete the last one from a queue to perform the pop operation.

基本思想是将n-1个元素(使n为元素总数)转移到其他队列,并从队列中删除最后一个元素以执行弹出操作。

  1. If q1 is not empty then transfer n-1 elements from q1 to q2 and DeQueue the last element and return it.

    如果q1不为空,则将n-1个元素从q1传输到q2,并对最后一个元素进行DeQueue并返回。

  2. If q2 is not empty then transfer n-1 elements from q2 to q1 and DeQueue the last element and return it.

    如果q2不为空,则将n-1个元素从q2传输到q1,并对最后一个元素进行DeQueue并返回。

int pop(struct stack *s){int count,size;
if(isempty(s->q2)){size=Size(s->q1);
count=0;
while(count<size-1){//transferring n-1 elements
EnQueue(s->q2,DeQueue(s->q1));
count++;
}
//last element to be popped
return DeQueue(s->q1);
}
else{size=Size(s->q2);
count=0;
while(count<size-1){EnQueue(s->q1,DeQueue(s->q2));
count++;
}
return DeQueue(s->q1);
}
}

Time complexity analysis:

时间复杂度分析:

  1. Push: O(1)

    推:O(1)

  2. Pop: O(n) , since transferring n-1 elements

    弹出:O(n),因为传输了n-1个元素

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

使用C ++的实现代码(使用STL) (Implementation code using C++ (using STL))

#include <bits/stdc++.h>
using namespace std;
struct Stack{queue<int> q1,q2;
void push(int x){if(q1.empty()){q2.push(x);    //EnQueue operation using STL
}
else{q1.push(x);    //EnQueue operation using STL
}
}
int pop(){int count,size,item;
if(q2.empty()){size=q1.size();            //size=no of elements;
count=0;
while(count<size-1){         //transfering n-1 elements
q2.push(q1.front());     // DeQueue operation using STL
q1.pop();
count++;
}
item=q1.front();
q1.pop();
return item;                 //popping out the element
}
else{size=q2.size();
count=0;
while(count<size-1){q1.push(q2.front());
q2.pop();
count++;
}
item=q2.front();
q2.pop();
return item;
}
}
};
int main()
{cout<<"implementing stack with two queues"<<endl;
cout<<"enter any integer to push and 0 to stop pushing"<<endl;
Stack s;
int x,count=0;
cin>>x;
while(x){s.push(x);
cin>>x;
count++;
}
cout<<"now popping......."<<endl;
while(count){cout<<s.pop()<<endl;
count--;
}
cout<<"executed successfully!!!"<<endl;
return 0;
}

Output

输出量

implementing stack with two queues
enter any integer to push and 0 to stop pushing
1
2
3
0
now popping.......
3
2
1
executed successfully!!!

翻译自: https://www.includehelp.com/data-structure-tutorial/implementation-of-stack-using-two-queues.aspx

如何使用两个堆栈实现队列

如何使用两个堆栈实现队列_使用两个队列实现堆栈相关推荐

  1. 两塑胶柱脚光纤端子_插销两个塑胶柱光纤端子_无塑胶柱光纤端子_无锁螺丝头光纤端子_塑胶双柱脚光纤端子_两塑胶柱无锁螺丝孔光纤端子

    两塑胶柱脚光纤座子_插销两个塑胶柱光纤座子_无塑胶柱光纤座子_无锁螺丝头光纤座子_塑胶双柱脚光纤座子_两塑胶柱无锁螺丝孔光纤座子 光纤耦合器TOSLINK是一种将数字电信号转换为光信号以传输数据的光传 ...

  2. 单片机实现环形队列_稀疏数组和队列(二)

    队列的介绍 队列以一种先入先出(FIFO)的线性表,还有一种先入后出的线性表(FILO)叫做栈. 教科书上有明确的定义与描述.类似于现实中排队时的队列(队尾进,队头出),队列只在线性表两端进行操作,插 ...

  3. mysql循环队列_数据结构:循环队列

    数据结构:循环队列 写在前面 数组表示的问题 对于队列最好的方法是使用链表实现,因为对于数组来说,队列可能会出现下面这种情况: 如图所示,不可以继续添加元素,否则会造成数组越界而遭致程序出错.然而此时 ...

  4. 让两个 mysql 自动同步_实现两个Mysql数据库之间同步的方案

    实现两个Mysql MySQL 为了实现replication 必须打开bin-log 项,也是打开二进制的MySQL 日志记录选项.MySQL 的bin log 二 进制日志,可以记录所有影响到数据 ...

  5. java 队列_用Java实现队列

    前言 队列是先进来的先出来 代码 package 总结 点击关注不迷路哦,后续有更新也会推送给您的.

  6. 两个列表合并去重_把两个pdf合并成一个如何解决?

    PDF文件在我们日常工作中使用的越来越多了,所有不可避免的我们会遇到要将多个PDF文件合并成为一个文件的情况,那么如何将两个PDF文件进行合并呢?可能大家都想要了解一下! 对于PDF文件这个问题,这里 ...

  7. 用同一uuid作为两个字段的值_这两个小技巧,让SQL语句不仅躲了坑,还提升了 1000 倍...

    作者:帅地 个人简介:一个热爱编程的在校生,我的世界不只有coding,还有writing.目前维护订阅号「苦逼的码农」,专注于写「算法与数据结构」,「Java」,「计算机网络」. 本次来讲解与 SQ ...

  8. 两个链表求交集_实现两个排序链表的并集和交集

    两个链表求交集 In computer science, a linked list is a linear collection of data elements, whose order is n ...

  9. c语言交换两个数字 位运算_交换两个8位数字| 8086微处理器

    c语言交换两个数字 位运算 Problem statement: 问题陈述: To swap two 8 bits numbers using third register on 8086 micro ...

最新文章

  1. 玩点深入的:Java 虚拟机内存结构及编码实战
  2. java 偶数求和 数组_JAVA实现幻方
  3. 有谁还用QuickReport吗?
  4. 为什么一个程序申请的内存有限制_为什么要做自己的小程序商城,做一个要多久?...
  5. http://www.ybtsoft.com/
  6. linux命令怎么调wsdl,如何从命令行执行SOAP wsdl Web服务调用
  7. mysql压力写入测试_mysql压力测试工具
  8. ZH奶酪:【阅读笔记】Deep Learning, NLP, and Representations
  9. Python 网络数据采集
  10. c语言怎样用vc绘图,大佬们,小菜鸟想问一问用vc编译器做简易画图软件
  11. 地面控制点的作用_地下室人防预留预埋施工要点及控制点
  12. VUE-地区选择器(V-Distpicker)
  13. 山东理工大学oj打字速度测试
  14. 网站域名空间服务器,网站 域名 空间 服务器
  15. 生活污水是怎么处理的
  16. 想给公司起个大气点的名字,大家帮忙啊!!!
  17. 团队管理那点破事!OKR绩效、核心人才、面试、技术分享、研发流程....
  18. 《基于C/S模式的android手机与PC机通信系统的开发》项目
  19. 分类计数原理与分步计数原理_分类计数原理与分步计数原理
  20. java仓库管理设计报告_仓库管理系统(课程设计JSPJAVA大学设计).doc

热门文章

  1. web文件怎么传到服务器,web文件传到服务器
  2. java开发环境搭建 pdf_01搭建java web开发环境.pdf
  3. JSON 使用 教程
  4. JS实现文本中查找并替换字符
  5. HTML邮件制作规范
  6. 原生JS实现的DOM操作笔记(草稿整理)
  7. 开发VUE使用第三库,发现有bug怎么办?
  8. python基础知识 - Day4
  9. python网络爬虫与信息提取 学习笔记day3
  10. IOS中设置全局变量