# leetcode [stack and queue - simple] 232. Stack implements queue

Keywords: Algorithm leetcode

# preface

Hello, I'm long Lu. I'm just a junior now. I'm working on the back end and occasionally on the front end. Now the main language is Java. For a long time before, I was learning some technologies of web development. I haven't studied and brushed questions like data structures and algorithms for a long time. I plan to pick them up and learn and do it well during this time.

During this period, I also happened to see that Cao Mao Lufei's blog added a self-study group. I happened to see that the blogger organized a leetcode question swiping and punching activity in the group. I also participated in it for one month. I intend to spend some time doing some questions every day and record them through a blog.

At present, there is a Github warehouse with leetcode: Code Capriccio leetcode brush questions , currently stack and queue topics.

# subject

Title source leetcode

leetcode address: 232. Implement queue with stack , difficulty: simple.

Title Description (from leetcode):

```Please use only two stacks to implement the first in first out queue. The queue should support all operations supported by the general queue( push,pop,peek,empty):

realization MyQueue Class:
void push(int x) Will element x Push to the end of the queue
int pop() Removes and returns an element from the beginning of the queue
int peek() Returns the element at the beginning of the queue
boolean empty() If the queue is empty, return true ；Otherwise, return false

explain:
You can only use standard stack operations -- that is, only push to top, peek/pop from top, size, and is empty The operation is legal.
Your language may not support stacks. You can use list perhaps deque(To simulate a stack, as long as it is a standard stack operation.

Can you achieve equal time sharing for each operation with a complexity of O(1) Queue? In other words, execution n The total time complexity of operations is O(n) ，Even if one of these operations may take a long time.

Example:
Input:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output:
[null, null, null, 1, 1, false]

Explanation:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

Tips:
1 <= x <= 9
Up to 100 calls push,pop,peek and empty
It is assumed that all operations are valid (for example, an empty queue will not be called) pop perhaps peek Operation)
```

Local debug code:

```class MyQueue {
public static void main(String[] args) {
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
System.out.println(queue.pop());
queue.push(5);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
}
}
```

# Problem solution

## NO1: dual stack implementation queue

Idea: one stack is used to enter the stack (stack 1), and the other stack is used to exit the stack (stack 2). When exiting the stack, first judge whether stack 2 is empty. If it is empty, uniformly exit the elements in stack 1 into stack 2 in turn. If it is not empty, this operation is not carried out to avoid some backward elements being pop out later.

code:

```class MyQueue {

private Stack<Integer> stack1;//Responsible for entering the stack
private Stack<Integer> stack2;//Responsible for out of stack

public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}

public void push(int x) {
stack1.push(x);
}

public int pop() {
dumpStack1();
return stack2.pop();
}

public int peek() {
dumpStack1();
return stack2.peek();
}

public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}

public void dumpStack1(){
//Judge whether stack 2 is empty, and only when it is empty will the elements of stack1 be pushed in (to avoid the backward elements entering stack 2)
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
}
}
```

# Reference articles

I am a long way. Thank you for your patience in reading. If you have any questions, please point them out and I will actively adopt them!
Welcome to my official account, long road Java, sharing Java learning articles and related materials.
Q group: 851968786 we can discuss and study together
Note: it can be reproduced, and the article link needs to be attached

Posted by rivasivan on Tue, 02 Nov 2021 12:17:37 -0700