This article mainly introduces PHP using two stacks to implement the queue function method, combined with the case analysis of PHP based on two stacks to implement the queue function of the idea and specific operation skills, the need for friends can refer to the next
The example in this article describes how PHP uses two stacks to implement the queue function. Share to everyone for your reference, as follows:
Problem
Implement a queue with two stacks to complete the push and pop operations of the queue. The elements in the queue are of type int.
Solution Ideas
Two stacks. When the stack is out, if stack 2 is not empty, the stack is 2. If the stack 2 is empty, then stack 1 of the stack into the stack 2.
Implementation code
<?PHP$ARR1 = Array (), $arr 2 = array (), function Mypush ($node) { Array_push ($arr 1, $node);} function Mypop () { if (!empty ($arr 2)) { return Array_pop ($arr 2); } else{ while (!empty ($arr 1)) { Array_push ($arr 2, Array_pop ($arr 1)); } Return Array_pop ($arr 2);} }
Articles you may be interested in:
A detailed explanation of the principles of PHP serialization and deserialization
The process of code implementation based on Swoole-Scan code login function
PHP7 extending the development of Hello Word implementation method