Summarize the ideas of some java programming problems

1. Use two stacks to implement a queue to complete the Push and Pop operations of the queue. The elements in the queue are of type int.

Ideas: The first stack is to simulate the entry of the queue value, and the second stack is to simulate the exit of the queue value. When the value is pushed into stack 1, it needs to push stack 2 as the value that is popped from the stack and then push it to stack 1. When the value is popped from stack 2, it needs to pop all the values ​​of stack 1 to stack 1. By doing these two operations, you can pop the stack. is to output the values ​​pushed onto the stack in order.

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325909249&siteId=291194637