美文网首页
队列、栈

队列、栈

作者: couriravant | 来源:发表于2019-12-18 19:33 被阅读0次

两个队列实现一个栈

image.png
public class TwoQueueImplStack {
    Queue<Integer> queue1 = new ArrayDeque<Integer>();
    Queue<Integer> queue2 = new ArrayDeque<Integer>();
    
    /*
     * 向栈中压入数据
     */
    public void push(Integer element){
        //两个队列都为空时,优先考虑 queue1
        if(queue1.isEmpty() && queue2.isEmpty()){
            queue1.add(element);
            return;
        }
        
        //如果queue1为空,queue2有数据,直接放入queue2
        if(queue1.isEmpty()){
            queue2.add(element);
            return;
        }
        
        //如果queue2为空,queue1有数据,直接放入queue1中
        if(queue2.isEmpty()){
            queue1.add(element);
            return;
        }
    }
    
    /*
     * 从栈中弹出一个数据
     */
    public Integer pop(){
        //如果两个栈都为空,则没有元素可以弹出,异常
        if(queue1.isEmpty() && queue2.isEmpty()){
            try{
                throw new Exception("satck is empty!");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        //如果queue1中没有元素,queue2中有元素,将其queue2中的元素依次放入queue1中,直到最后一个元素,弹出即可
        if(queue1.isEmpty()){
            while(queue2.size() > 1){
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }
        
        //如果queue2中没有元素,queue1中有元素,将其queue1中的元素依次放入queue2中,直到最后一个元素,弹出即可
        if(queue2.isEmpty()){
            while(queue1.size() > 1){
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }
        
        return (Integer)null;
    }
    
    public static void main(String[] args) {
        TwoQueueImplStack qs = new TwoQueueImplStack();
        qs.push(2);
        qs.push(4);
        qs.push(7);
        qs.push(5);
        System.out.println(qs.pop());
        System.out.println(qs.pop());
        
        qs.push(1);
        System.out.println(qs.pop());
    }
}

两个栈实现一个队列

image.png
public class TwoStackImplQueue {
 
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    /*
     * 队列的数据压入过程
     */
    public void push(Integer element){
        stack1.push(element);
    }
    
    /*
     * 队列的数据弹出过程
     */
    public Integer pop(){
        if(stack2.size() <= 0){ //第二个栈为空
            while(stack1.size() > 0){   //第一个栈不为空
                stack2.push(stack1.pop());  //将其第一个栈的数据压入第二个栈中
            }
        }
        if(stack2.isEmpty()){
            try{
                throw new Exception("queue is empty");
            }catch(Exception e){
                //e.printStackTrace();
            }
        }
        Integer head = stack2.pop();
        return head;
    }
    
    public static void main(String[] args) {
        TwoStackImplQueue sq = new TwoStackImplQueue();
        sq.push(1);
        sq.push(3);
        sq.push(5);
        sq.push(4);
        sq.push(2);
        
        System.out.println(sq.pop());
        System.out.println(sq.pop());
        
        sq.push(7);
        System.out.println(sq.pop());
    }
}

相关文章

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 栈和队列

    用栈定义队列(出入栈) 用队列定义栈(数据队列和辅助队列)

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • Swift 队列&栈 相关操作

    栈 LIFO(后进先出) 队列 FIFO(先进先出) 队列与栈相互的实现 栈 - 队列实现 队列 - 栈实现 相关...

  • 栈和队列

    栈和队列 本质上是稍加限制的线性表 栈和队列定义 栈顺序栈定义 链栈结点定义 队列顺序队列 链队列链队类型定义 链...

  • 算法-栈和队列算法总结

    栈和队列算法总结 1 模拟 1.1 使用栈实现队列 1.2 使用队列实现栈 2 栈的应用 2.1 栈操作 2.2 ...

  • 栈&队列

    一、栈&队列总结 栈/队列的应用接雨水验证栈序列滑动窗口的最大值 栈/队列的特殊实现用两个栈实现队列用两个队列实现...

  • 算法分析 [BFS、Greedy贪心] 2019-02-18

    队列 和 栈 232. 用栈实现队列 Implement Queue using Stacks双栈,出队列时,将i...

  • 队列之-队列实现栈

    一、队列实现栈核心算法概述 之前已经描述过了用栈实现队列的功能,见栈系列之-实现队列,那么同样队列也可以用来实现栈...

  • 数据结构 | 其三 栈和队列

    栈 java中栈继承了Vector 队列 2.1 普通队列 2.2 循环队列 2.3 优先级队列 2.4 阻塞队列...

网友评论

      本文标题:队列、栈

      本文链接:https://www.haomeiwen.com/subject/quaunctx.html