题目1512:用两个栈实现队列
来源:互联网 发布:管家婆软件打印设置 编辑:程序博客网 时间:2024/04/29 20:46
- 题目描述:
用两个栈来实现一个队列,完成队列的Push和Pop操作。
队列中的元素为int类型。
- 输入:
每个输入文件包含一个测试样例。
对于每个测试样例,第一行输入一个n(1<=n<=100000),代表队列操作的个数。
接下来的n行,每行输入一个队列操作:
1. PUSH X 向队列中push一个整数x(x>=0)
2. POP 从队列中pop一个数。
- 输出:
对应每个测试案例,打印所有pop操作中从队列pop中的数字。如果执行pop操作时,队列为空,则打印-1。
- 样例输入:
3PUSH 10POPPOP
- 样例输出:
10-1
有一个case超时, 真不知道什么原因:
#include<iostream>#include<stack>#include<string.h>using namespace std;class myQueue{ public: void en(int x); int de(); int sizeq(); bool emptyq(); private: stack<int>v1; stack<int>v2;};int myQueue::sizeq(){ return v1.size() + v2.size();}bool myQueue::emptyq(){ if(sizeq()==0) return true; return false;}void myQueue::en(int x){ v1.push(x);}int myQueue::de(){ if(sizeq()==0) return -1; //the queue is empty int front_val; if(v1.size()==1) { front_val = v1.top(); v1.pop(); return front_val; } while(v1.size()!=1) { front_val = v1.top(); v2.push(front_val); v1.pop(); } front_val = v1.top(); v1.pop(); int temp_val; while(!v2.empty()) { temp_val = v2.top(); v1.push(temp_val); v2.pop(); } return front_val;}int main(){ int n; int val; char str[5]; myQueue qu; while(cin>>n) { while(n--) { cin>>str; if(!strcmp(str,"PUSH")) { cin>>val; qu.en(val); } if(!strcmp(str,"POP")) cout<<qu.de()<<endl; } } return 0;}
0 0
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目1512:用两个栈实现队列
- 题目:用两个栈实现队列
- 算法题目---用两个栈实现队列
- 题目1512:用两个栈实现队列-九度
- 【九度】题目1512:用两个栈实现队列
- 九度oj 题目1512:用两个栈实现队列
- 九度_题目1512:用两个栈实现队列
- 九度OJ-题目1512:用两个栈实现队列
- 题目7:用两个栈实现一个队列。
- 算法题目---用两个队列实现一个栈
- 题目1512:用两个栈实现队列 && 包含min函数的栈
- 制作绿色版的MSDN2008 WINCE5 WINCE6等HELP2帮助系统
- Leetcode: Merge Two Sorted Lists
- MFC选择文件夹的对话框
- Oracle提高sql执行效率的建议(2)
- 顺着tpl_index_default.php摸到的瓜
- 题目1512:用两个栈实现队列
- idea6410裸调led蜂鸣器的方法
- Androd修改app_process为Zygote进程名字
- 我的架构师历程,其实一切只是源于我想要一台属于自己的笔记本(上)
- linux shell if 参数
- 织梦CMS,当前栏目高亮显示
- DedeCMS Error:Tag disabled:"php"的解决办法
- 写一个函数判断两个字符串是否是变位词
- 黑马程序员——银行业务调度系