标签:als fun 一个栈 ret pop 技术分享 函数 get png
<?php header("content-type:text/html;charset=utf-8"); /* * 仅用递归函数和栈操作逆序一个栈 P8 */ function getAndRemoveLastElement(SplStack $stack){ if($stack->isEmpty()){ return false; } $value = $stack->pop(); if($stack->isEmpty()){ return $value; } else{ $last = getAndRemoveLastElement($stack); $stack->push($value); return $last; } } function reverse(SplStack $stack){ if($stack->isEmpty()){ return false; } else{ $value = getAndRemoveLastElement($stack); reverse($stack); $stack->push($value); } } echo "原栈:"; echo "</br>"; $stack = new SplStack(); $stack->push(1); $stack->push(7); $stack->push(0); $stack->push(2); print_r($stack); echo "</br>"; echo "逆序后的栈:"; echo "</br>"; reverse($stack); print_r($stack);
输出结果:
标签:als fun 一个栈 ret pop 技术分享 函数 get png
原文地址:https://www.cnblogs.com/xlzfdddd/p/10021886.html