剑指Offer——Java实现栈和队列的互模拟操作

来源:互联网 发布:ubuntu mv 目录非空 编辑:程序博客网 时间:2024/05/17 04:05

剑指Offer——Java实现栈和队列的互模拟操作

栈模拟队列

  题目:JAVA实现用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。
  思路:其实就是把队列正常入栈,出栈的时候先把栈里的内容按顺序搬到另一个栈里,负负得正,这样再按顺序出栈的时候,就成了入栈前队列的顺序

package cn.edu.ujn.demo;import java.util.Stack;import org.junit.Test;public class Stack2Queue {    Stack<Integer> stack1 = new Stack<Integer>();    Stack<Integer> stack2 = new Stack<Integer>();    // 入栈函数    public void push(int num) {        stack1.push(num); // 要往栈中压入什么就直接用栈的push方法就好了    }    // 出栈函数    public int pop() {        Integer re = null;        if (!stack2.empty()) { // 如果栈2不是空的,那么把最上面那个取出来            re = stack2.pop();        } else {            // 如果栈2是空的,就把栈1里的数一个个取出来,放到栈2里            while (!stack1.empty()) {                re = stack1.pop();                stack2.push(re);            }            // 栈2里有数之后,再次把里面的数取出来            if (!stack2.empty()) {                re = stack2.pop();            }        }        return re;    }    @Test    public void test(){        Stack2Queue sq = new Stack2Queue();        sq.push(1);        sq.push(3);        System.out.println("...:" + sq.pop());        System.out.println("...:" + sq.pop());    }}

队列模拟栈

 题目:JAVA实现用两个队列来实现一个栈,完成栈的Push和Pop操作

package cn.edu.ujn.demo;import java.util.LinkedList;public class Queue2Stack {    private LinkedList<String> queue1;    private LinkedList<String> queue2;    public Queue2Stack(){        queue1 = new LinkedList<String>();        queue2 = new LinkedList<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) {         Queue2Stack stack = new Queue2Stack();              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       }}

这里写图片描述
这里写图片描述
这里写图片描述

2 0
原创粉丝点击