Two stacks queue, the queue to achieve two stacks --- Java

Copyright notice: reproduced, please indicate the source of downstream https://blog.csdn.net/Hollake/article/details/91041199

Two stacks queue

Using two stacks, a stack push, pop a stack, each time only when the push directly into the stack push, pop each time, as long as only popped from the stack pop, pop the stack is null when the push stack the push to pop all the elements of the stack, thus reducing the added element order, FIFO, note that only the pop stack is null when to push, if not will be out of order, it will not reach the FIFO the goal of. For example:

  • push stack Add 1,2,3,4,5.
  • Now pop operation is required, then pop the stack to ensure that the case where the null all the elements added will pop the stack to pop the stack pop, pop order 5,4,3,2,1, then pop the stack order of the elements are also added as 5,4,3,2,1, and then directly to pop the top element 1, in line with FIFO.
class TwoStackQueue {
    private Stack<Integer> pushStack;
    private Stack<Integer> popStack;
    TwoStackQueue() {
        pushStack = new Stack<>();
        popStack = new Stack<>();
    }

    public void push(int num) {
        pushStack.push(num);
    }

    public int pop() {
        if (pushStack.isEmpty() && popStack.isEmpty()) {
            throw new RuntimeException("Queue is empty!");
        }
//        如果popStack为空,那么就将pushStack中的元素全部压入到popStack
        if (popStack.isEmpty()) {
            while ( !pushStack.isEmpty() ) {
                popStack.push(pushStack.pop());
            }
        }
//        返回popStack的栈顶元素
        return popStack.pop();
    }
    public int peek(){
        if (pushStack.isEmpty() && popStack.isEmpty()) {
            throw new RuntimeException("Queue is empty!");
        }
        if (popStack.isEmpty()) {
            while ( !pushStack.isEmpty() ) {
                popStack.push(pushStack.pop());
            }
        }
        return popStack.peek();
    }
}

Two stacks achieved queue

Using two stacks, a queue, a Help, each push directly into the queue when the queue, when the pop directly in queue element poll () to only one element, the remaining elements of a poll () out on the line, while the remaining elements help add to the queue, and note that after each pop peek or two queues index must be exchanged. For example:

  • 1,2,3,4,5 to push the queue
  • pop operation: Queue queue sequentially adding it to help eject the queue, 1,2,3,4 1,2,3,4 elements help now, then the only remaining stresses queue element 5 a pop-up, in line with LIFO.
  • swap () to exchange queue and help index, so there will be new elements to continue to add.
class TwoQueueStack {
    private Queue<Integer> queue;
    private Queue<Integer> help;
    TwoQueueStack(){
        queue = new LinkedList<>();
        help = new LinkedList<>();
    }
    public void push(int num) {
        queue.add(num);
    }
    public int pop(){
        if (queue.isEmpty()) {
            throw new RuntimeException("Stack is empty!");
        }
        while ( queue.size() > 1 ) {
            help.add(queue.poll());
        }
        int res = queue.poll();
        swap();
        return res;
    }
    public int peek(){
        if (queue.isEmpty()) {
            throw new RuntimeException("Stack is empty!");
        }
        while ( queue.size() > 1 ) {
            help.add(queue.poll());
        }
        int res = queue.poll();
        help.add(res);
        swap();
        return res;
    }
    private void swap() {
        Queue<Integer> tmp = help;
        help = queue;
        queue = tmp;
    }
}

 

Guess you like

Origin blog.csdn.net/Hollake/article/details/91041199