栈,队列 以及 stack, queue 的相互实现
来源:互联网 发布:淘宝机锋市场怎么样 编辑:程序博客网 时间:2024/06/04 19:39
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Stack;
- /*
- * Q 57 用两个栈实现队列
- */
- public class QueueImplementByTwoStacks {
- private Stack<Integer> stack1;
- private Stack<Integer> stack2;
- QueueImplementByTwoStacks(){
- stack1=new Stack<Integer>();
- stack2=new Stack<Integer>();
- }
- public Integer poll(){
- Integer re=null;
- if(!stack2.empty()){
- re=stack2.pop();
- }else{
- while(!stack1.empty()){//move to stack2 to make stack1 have only one element.Then pop this element.
- re=stack1.pop();
- stack2.push(re);
- }
- if(!stack2.empty()){
- re=stack2.pop();
- }
- }
- return re;
- }
- public Integer offer(int o){
- stack1.push(o);
- return o;
- }
- public static void main(String[] args) {
- QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks();
- List<Integer> re=new ArrayList<Integer>();
- queue.offer(1);
- queue.offer(2);
- queue.offer(3);
- re.add(queue.poll());
- queue.offer(4);
- re.add(queue.poll());
- queue.offer(5);
- re.add(queue.poll());
- re.add(queue.poll());
- re.add(queue.poll());
- System.out.println(re.toString());
- }
- }
- import java.util.LinkedList;
- /*
- * Q 57 用两个队列实现一个栈
- */
- public class StackImplementByTwoQueues {
- //use 'queue1' and 'queue2' as a queue.That means only use the method 'addLast' and 'removeFirst'.
- private LinkedList<Integer> queue1;
- private LinkedList<Integer> queue2;
- StackImplementByTwoQueues(){
- queue1=new LinkedList<Integer>();
- queue2=new LinkedList<Integer>();
- }
- public Integer pop(){
- Integer re=null;
- if(queue1.size()==0&&queue2.size()==0){
- return null;
- }
- if(queue2.size()==0){
- while(queue1.size()>0){
- re=queue1.removeFirst();
- if(queue1.size()!=0){//do not add the last element of queue1 to queue2
- queue2.addLast(re);
- }
- }
- }else if (queue1.size()==0){
- while(queue2.size()>0){
- re=queue2.removeFirst();
- if(queue2.size()!=0){//do not add the last element of queue2 to queue1
- queue1.addLast(re);
- }
- }
- }
- return re;
- }
- public Integer push(Integer o){
- if(queue1.size()==0&&queue2.size()==0){
- queue1.addLast(o);//queue2.addLast(o); is also ok
- }
- if(queue1.size()!=0){
- queue1.addLast(o);
- }else if(queue2.size()!=0){
- queue2.addLast(o);
- }
- return o;
- }
- public static void main(String[] args) {
- StackImplementByTwoQueues stack=new StackImplementByTwoQueues();
- int tmp=0;
- stack.push(1);
- stack.push(2);
- stack.push(3);
- tmp=stack.pop();
- System.out.println(tmp);//3
- stack.push(4);
- tmp=stack.pop();
- System.out.println(tmp);//4
- tmp=stack.pop();
- System.out.println(tmp);//2
- stack.push(5);
- stack.push(6);
- tmp=stack.pop();
- System.out.println(tmp);//6
- tmp=stack.pop();
- System.out.println(tmp);//5
- tmp=stack.pop();
- System.out.println(tmp);//1
- }
- }
- 栈,队列 以及 stack, queue 的相互实现
- 栈,队列 以及 stack, queue 的相互实现 .
- 使用栈Stack实现队列Queue
- 利用栈Stack实现队列(Queue)
- python实现stack(栈)和队列(queue)
- Java实现栈Stack和队列Queue
- STL之栈stack,队列queue以及优先队列
- stack 栈 queue 队列
- 栈Stack和队列Queue
- 栈stack与队列queue
- 栈Stack和队列Queue
- 面试题---使用栈stack实现队列queue
- java实现数据结构——栈Stack与队列Queue
- Java实现数据结构栈stack和队列Queue
- 用两个栈(Stack)实现一个队列(Queue)
- 用两个栈(Stack)实现一个队列(Queue)
- 用两个栈(Stack)实现一个队列(Queue)
- 自己实现一个栈(Stack)和队列(Queue)
- Wireshark图解教程(简介、抓包、过滤器)【转】
- leetcode代码分类汇总之-递归
- 从 ADO“经典”迁移到 ADO.NET
- Android studio for mac
- 临界区(Critical section)与互斥体(Mutex)的区别
- 栈,队列 以及 stack, queue 的相互实现
- acm 使用穷举法解决人鸡狗米过河的问题
- Objective-C中的id类型
- 希尔排序
- Face Database
- ubuntu更新文件删掉的命令
- 关于win7系统的jdk时间与系统时间相差8小时
- Milanoo首席技术官肖江《从大学生走到CTO》全国巡讲川大站圆满结束
- ThinkPHP 中使用 PHPMailer 发送邮件(支持163、QQ邮箱,附下载)