Blogger Information
Blog 16
fans 0
comment 0
visits 14625
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
线性表的php实现
进击的小菜鸟
Original
1176 people have browsed it

实例

<?php

class straitList{
    public $list;

    /**
     * 初始化.
     * @param $array
     */
    public function __construct($array)
    {
        $this->list = $array;
    }

    /**
     * 线性表是否为空
     * @return bool
     */
    public function listEmpty()
    {
        return empty($this->list);
    }

    /**
     * 情况线性表
     */
    public function clearList()
    {
        unset($this->list);
    }

    /**
     * @param $item 线性表的序号
     * @return bool
     */
    public function getElem($item)
    {
        return array_key_exists($item, $this->list) ? $this->list[$item] : false;
    }

    /**
     * @param $v 元素值 返回元素在线性表中匹配到的第一个序号或者false
     * @return bool|false|int|string
     */
    public function locateElem($v)
    {
        $re = array_search($v, $this->list);
        return $re === false ? false : $re;
    }

    /**
     * @param $item 插入的位置
     * @param $value 要插入的元素
     * @return bool
     */
    public function listInsert($item, $value)
    {
        $len = $this->listLength();
        if ($item < 1 || $item > $len) {
            return false;
        }
        for ($i = $len-1; $i >$item-1; $i--) {
            $this->list[$i+1] = $this->list[$i];
        }
        $this->list[$item-1] = $value;
        return true;
    }

    /**
     * @param $item 要删除的元素序号
     * @return bool
     */
    public function ListDelete($item)
    {
        $len = $this->listLength();
        if ($item < 1 || $item > $len) {
            return false;
        }
        $return = $this->list[$item-1];
        for ($i = $item-1; $i < $len; $i++) {
            $this->list[$i] = $this->list[$i+1];
        }
        unset($this->list[$len-1]);
        return $return;
    }

    /**
     * @return int
     */
    public function listLength()
    {
        return count($this->list);
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!