如何用栈实现一个队列?

栈是先进后出,队列是先进先出,所以两个栈,一个接收数据,一个处理并返回数据,出来就是一个队列,先进先出的形式了。

如图所示:
如何用栈实现一个队列?_第1张图片

代码实现:

package com.revision.Stack;

import java.util.Queue;
import java.util.Stack;

class Test {


    public static void main(String[] args) {
        Stack2Queue stack2Queue = new Stack2Queue();
        stack2Queue.push(1);
        stack2Queue.push(2);
        stack2Queue.push(3);
        stack2Queue.pop();
        stack2Queue.push(4);
        stack2Queue.pop();
        stack2Queue.pop();
        stack2Queue.pop();
    }


}

public class Stack2Queue {
    //作为入队列
    Stack<Integer> stack1 = new Stack<>();
    //作为出队列
    Stack<Integer> stack2 = new Stack<>();

    public void push(int num) {
        //push时先看stack2中有无数据,有的话就先放回给stack1中
        while (!stack2.empty()) {
            stack1.push(stack2.peek());
            stack2.pop();
        }
        //无数据就把数值先放进stack1中
        stack1.push(num);
        System.out.println("入队元素为:" + num);
    }

    public int pop(){
        //出队时要保证stack1为空,否则顺序出错
        while(!stack1.empty()){
            stack2.push(stack1.peek());
            stack1.pop();
        }
        int tmp = stack2.peek();
        System.out.println("出队元素为:" + tmp);
        stack2.pop();
        return tmp;
    }

}

你可能感兴趣的:(java,数据结构,java,se,栈,队列,用栈实现队列)