資料結構隨筆-php實現棧

來源:互聯網
上載者:User

標籤:基本   ack   返回   orm   php   bool   ott   元素   等於   

棧(Stack)滿足後進先出(LIFO)的原則;

下面利用php實現棧的相關操作;

本執行個體棧的基本操作:

入棧(push):向棧內壓入一個元素,棧頂指標指向棧頂元素

出棧(pop): 從棧頂去除元素,棧頂指標向棧底移動;

判空(isEmpty):當棧頂指標恢複為初始指標狀態即為空白棧;

判滿(isFull): 當棧頂等於容量減一則棧滿;

 

使用php數組實現一個簡單操作的棧;

<?php/** * Created by PhpStorm. * User: gordon * Date: 2017/8/6 * Time: 12:58 *//** * Class Stack 簡單棧 */class Stack{    /**     * 棧初始指標     */    const TOP_INIT = -1;    /**     * 棧底指標     */    const BOTTOM = 0;    /**     * @var array 棧數組     */    private $_stack;    /**     * @var int 棧頂指標     */    private $_stackTop;    /**     * @var int 棧的容量     */    private $_stackSize;    /**     * Stack constructor. 初始化棧     * @param int $size 棧的容量大小;     */    public function __construct($size)    {        $this->_stack = [];        $this->_stackSize = $size;        /**         * 棧頂初始狀態位於棧底         */        $this->_stackTop = self::TOP_INIT;    }    /**     * @method 銷毀棧(非必需)     */    public function __destruct()    {        unset($this->_stack);    }    /**     * @method 入棧     * @param mixed $elem 入棧元素     * @return bool     */    public function push($elem)    {        if ($this->isFull()) {            return false;        }        $this->_stack[++$this->_stackTop] = $elem;        return true;    }    /**     * @method 出棧     * @return mixed|null 返回棧頂的元素或者當棧為空白時返回null     */    public function pop()    {        if ($this->isEmpty()) {            return null;        }        $result = $this->_stack[$this->_stackTop];        unset($this->_stack[$this->_stackTop]);        $this->_stackTop--;        return $result;    }    /**     * @method 判斷棧是否為空白;     * @return bool     */    public function isEmpty()    {        return $this->_stackTop === self::TOP_INIT;    }    /**     * @method 判斷棧是否達到最大;     * @return bool     */    public function isFull()    {        return $this->_stackSize - 1 === $this->_stackTop;    }    /**     * @method 清空棧     */    public function clearStack()    {        $this->_stack = [];        $this->_stackTop = self::TOP_INIT;    }    /**     * @method 遍曆棧(測試用);     */    public function outputStack()    {        echo ‘take traversal stack from bottom to top‘.PHP_EOL;        for ($i = self::BOTTOM; $i < $this->_stackTop + 1; $i++) {            echo $this->_stack[$i].PHP_EOL;        }        echo PHP_EOL;        echo PHP_EOL;        echo ‘take traversal stack from top to bottom‘.PHP_EOL;        for ($i = $this->_stackTop; $i >= self::BOTTOM; $i--) {            echo $this->_stack[$i].PHP_EOL;        }    }}

測試一下棧操作類

$s = new Stack(4);$s->push(1);$s->push(2);$s->push(3);$s->push(4);$s->push(5);$s->outputStack();  //stack from top to bottom: 4 3 2 1$s->pop();          //output:4  stack: 3 2 1$s->pop();          //output:3  stack: 2 1$s->outputStack();  //stack: 2 1$s->push(5);$s->outputStack();  //stack: 5 2 1$s->clearStack();var_dump($s->pop());    //null;

經過測試代碼很講道理;

 

如有不對,敬請指教

資料結構隨筆-php實現棧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.