use 2 stacks to simulate a queue
class Stack{
private:
int cur = ;
int elem[];
public:
void push(int n);
int pop();
int peek();
int size();
};
void Stack::push(int n){
elem[cur] = n;
cur++;
}
int Stack::pop(){
cur--;
return elem[cur + ];
}
int Stack::peek(){
return elem[cur - ];
}
int Stack::size(){
return cur;
}
class Queue{
private:
Stack s1, s2;
public:
void enqueue(int n);
int dequeue();
int peek();
int size();
};
void Queue::enqueue(int n){
if(s1.size() == ){
while(s2.size()){
s1.push(s2.pop());
}
}
s1.push(n);
}
int Queue::dequeue(){
if(s2.size() == ){
while(s1.size()){
s2.push(s1.pop());
}
}
return s2.pop();
}
int Queue::peek(){
if(s2.size() == ){
while(s1.size()){
s2.push(s1.pop());
}
}
return s2.peek();
}
int Queue::size(){
return s1.size()? s1.size() : s2.size();
}
use 2 stacks to simulate a queue的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- LintCode-Implement Queue by Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- 40. Implement Queue by Two Stacks【medium】
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...
随机推荐
- 1951: [Sdoi2010]古代猪文
1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2171 Solved: 904[Submit][Status] ...
- web service client端调用服务器接口
打开项目的web service client 其中wsdl URL http://www.51testing.com/html/55/67755-848510.html 去这里面查找一些公开的 ...
- object model 概述
Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...
- joda-time的一个DEMO
Date activeDate = person.getActiveTime(); if(activeDate==null){ modelMap.put("expireDate", ...
- Ansible Lookup
1. 文件内容的读取 --- - hosts: all vars: contents: "{{ lookup('file', '/etc/foo.txt') }}" tasks: ...
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- easyui datagrid 的排序问题
今日遇到一个datagrid排序问题,sortable,sorter函数都已设置,但是始终没有效果,无法在界面自定义排序.后来发现,需要设置remoteSort:false. 切记!!! remote ...
- Linux文件系统简介及常用命令
在linux系统中一切皆是文件,下面简要总结了一下linux文件系统中分区类型.文件系统类型以及常用命令. 一.分区类型1.主分区:最多只能有四个2.扩展分区:只能有一个,也可以看做是主分区的一种.即 ...
- hibernate主键generator属性介绍
increment(递增) 用于为long, short或者int类型生成唯一标识.只有在没有其他进程往同一张表中插入数据时才能使用. 在集群下不要使用. identity (标识)对DB2,MySQ ...
- log4j.properties 的使用详解
一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...