java 两个栈实现队列与两个列实现栈

来源:互联网 发布:mysql配置文件my.ini 编辑:程序博客网 时间:2024/05/20 19:46

一、两个栈模拟队列

importjava.util.Stack;publicclass Solution {               Stack<Integer>stack1 = new Stack<Integer>();            Stack<Integer> stack2 = new Stack<Integer>();            public void push(int node) {                stack1.push(new Integer(node));            }            public int pop() {                     if(stack2.isEmpty()){                             while(!stack1.isEmpty()){                                      stack2.push(stack1.pop());                             }                     }                     if(stack2.isEmpty()){                             System.out.println("stack1is empty!");                     }                     returnstack2.pop().intValue();            }}


二、两个队列模拟栈

importjava.util.LinkedList;publicclass StackTwoQueue {         private LinkedList<String>queue1;         private LinkedList<String>queue2;         public StackTwoQueue(){                  queue1 = newLinkedList<String>();                  queue2 = newLinkedList<String>();         }         public String pop(){                  String 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){                                            queue2.addLast(re);                                   }                          }                  }else if(queue1.size() == 0){                          while(queue2.size()>0){                                   re =queue2.removeFirst();                                   if(queue2.size()!=0){                                            queue1.addLast(re);                                   }                          }                  }                  return re;         }         public String push(String str){                  if(queue1.size() ==0&& queue2.size() == 0){                          queue1.addLast(str);                  }                  if(queue1.size()!=0){                          queue1.addLast(str);                  }else if(queue2.size()!=0){                          queue2.addLast(str);                  }                  return str;         }                  public static void main(String[] args) {                    StackTwoQueue stack=new StackTwoQueue();                 String tmp;                 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           }}


0 0