PHP實現線性表的順序儲存結構

來源:互聯網
上載者:User
classSqList{public$elem;    public$length;    public$size;}classLinear{const LIST_INIT_SIZE = 10;    const LIST_INCREMENT = 5;    private$list = null;    /**     * 構造一個空的線性表     */publicfunctioninitList()    {$this->list = new SqList();        $this->list->elem = array();        $this->list->length = 0;        $this->list->size = self::LIST_INIT_SIZE;        returntrue;    }    /**     * 銷毀線性表     */publicfunctiondestoryList()    {if (is_object($this->list)) {            $this->list = null;        }    }    /**     * 是否為空白表     */publicfunctionlistEmpty()    {if (is_object($this->list)) {            return$this->list->length == 0 ? true : false;        }    }    /**     * 返回元素個數     */publicfunctionlistLength()    {if (is_object($this->list)) {            return$this->list->length;        }    }    /**     * 擷取指定位置的元素     */publicfunctiongetElem($i)    {if ($i < 1 || $i > $this->list->length + 1) {            returnfalse;        }        return$this->list->elem[$i-1];    }    /**     * 在指定位置插入元素     */publicfunctionlistInsert($i, $e)    {if ($i < 1 || $i > $this->list->length + 1) {            returnfalse;        }        if ($this->list->length >= $this->list->size) {            $this->list->size += self::LIST_INCREMENT;        }        for ($j = $this->list->length; $j >= $i; $j--) {            $this->list->elem[$j] = $this->list->elem[$j-1];        }        $this->list->elem[$i-1] = $e;        $this->list->length++;    }    /**     * 刪除指定位置資料元素     */publicfunctionlistDelete($i)    {if ($i < 1 || $i > $this->list->length) {            returnfalse;        }        $data = $this->list->elem[$i-1];        for ($j = $i -1; $j < $this->list->length -1; $j++) {            $this->list->elem[$j] = $this->list->elem[$j+1];        }        unset($this->list->elem[$this->list->length-1]);        $this->list->length--;        return$data;    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了PHP實現線性表的順序儲存結構,包括了方面的內容,希望對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.