Recently often asked the stack and queue algorithm problem, previously on the list, the binary tree has the understanding, stacks and queues feel a bit confused, and later looked at the discovery is to use the array simulation FIFO and advanced after the situation.
It's better to ask this than to ask the list of the moment.
The following return to the point: First look at the stack
<?PHPclassstack{ Public Static $dataInfo=Array(); Public Static $theMax= 10; Public Static $length=0; // Public Static $stackInfo; Public functionAddData ($data){ if(Self::$length>= Self::$theMax){ return false; }Else{ Array_push(Self::$dataInfo,$data); Self::$length++; } } Public functionOutdata () {if(Self::$length<=0 ){ return false; }Else{ $data=Array_pop(Self::$dataInfo); Self::$length--; return $data; } } Public functionGetAll () {foreach(Self::$dataInfo as $value) { //Code: Print_r($value); } } }$a=NewStack ();$a->adddata (1);$a->adddata (3);$a->adddata (5);$a-getAll ();Print_r($a-outdata ());?>
Similarly, the queue is the same, but you can change Array_pop () to Array_shift ().
Have any questions please feel free to ask questions.
Stack and queue implementations (PHP)