数据结构之线性表(顺序存储表)

php

<?php

/**
 * Created by PhpStorm.
 * User: SillyCat
 * Date: 2024/3/2
 * Time: 18:47
 */
class SequenceList
{
    private $item = array();
    private $length = 0;

    public function __construct()
    {
//        $this->item = $item;
        $this->length = 0;
    }

    /**
     * @function: get_length
     * @Desc:获取表的长度
     * @return int
     */
    public function get_length()
    {
        return $this->length;
    }

    /**
     * @function: insert
     * @Desc:在指定位置插入元素
     * @param $index  /要插入的位置
     * @param $item /要插入的元素
     * @return bool 插入成功返回true,否则返回false
     */
    public function insert($index,$item)
    {
        if($index<0||$index>$this->length)
        {
            return false;
        }
        // 元素后移:将数字统统往后移动一个位置
        //方法一
//        for ($i = $this->length - 1; $i >= $index; $i--) {
//            $this->item[$i + 1] = $this->item[$i]; //
//        }
        //array_splice 函数 用于插入元素
        array_splice($this->item,$index,0,$item);
        $this->length++;
        return true;
    }

    /**
     * @function: delete
     * @Desc:删除指定位置的元素
     * @param $index
     * @return mixed|null 删除成功返回删除的元素,否则返回null
     */
    public function delete($index)
    {
        if($index<0||$index>$this->length-1)
        {
            return null;//删除失败
        }
        $item = $this->item[$index];
//元素前移
//        for ($i = $index; $i < $this->length - 1; $i++) {
//            $this->item[$i] = $this->item[$i + 1];
//        }
        array_splice($this->item,$index,1);
        $this->length--;
        return $item;

    }
    /**
     * @function: get_item_by_index
     * @Desc:根据位置获取元素
     * @param $index
     * @return mixed 不存在返回null
     */
    public function get_item_by_index($index)
    {
        $length= $this->get_length();
        if($index<0||$index>$length-1)
        {
            return null;
        }
        return $this->item[$index];
    }
    /**
     * @function: get_index_by_item
     * @Desc:根据元素获取位置
     * @param $item
     * @return int 位置 -1 不存在 否则返回位置
     */
    public function get_index_by_item($item)
    {
        $length= $this->get_length();
        for($i=0;$i<$length;$i++)
        {
            if($this->item[$i]==$item)
            {
                return $i;
            }
        }
        return -1;
    }
       /**
        * @function: display
        * @Desc:显示线性表
        */
    public function display()
    {
        $length= $this->get_length();
        for($i=0;$i<$length;$i++)
        {
            echo $this->item[$i]." ";
        }
        echo PHP_EOL;
    }

}

/**
 * 测试
 */
$line=new SequenceList();
// 插入元素
$line->insert(0,1);
$line->insert(1,2);
$line->insert(2,3);
$line->insert(3,4);
$line->insert(4,5);
//显示
$line->display();//1 2 3 4 5
//指定位置的元素
echo $line->get_item_by_index(2).PHP_EOL;//3
//指定元素的位置
echo $line->get_index_by_item(3).PHP_EOL;//2
//删除指定位置的元素
echo $line->delete(2).PHP_EOL;//3
posted @   傻乎乎的猫  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示