1. Question: Use two stacks to implement a queue. First use a stack to enter data, and then output this stack to another stack to form a first-in-first-out order for the queue.
Because PHP’s array can simulate the implementation of a stack, the code is as follows:
<?php function StackToQueue($data) { $arr=array(); while($_t=array_pop($data)) { array_push($arr, $_t); } return $arr; } $a=array(); for($i=1;$i<=5;$i++) { array_push($a, $i); } var_dump($a); $a=StackToQueue($a); var_dump($a);
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces queues and stacks: two stacks implement queues, and two queues implement stacks, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.