> 백엔드 개발 > PHP 튜토리얼 > 数据结构 - 线性表学习(php模拟)

数据结构 - 线性表学习(php模拟)

WBOY
풀어 주다: 2016-06-13 12:29:19
원래의
888명이 탐색했습니다.

数据结构 --- 线性表学习(php模拟)

线性表:零个或多个数据元素的有限序列(注:以下都是用的整型数据模拟)

一 顺序存储结构(用一段地址连续的存储单元一次存储线性表的数据元素)
  1.1 三个属性:存储空间的起始位置;最大存储容量;当前长度
  注:数组长度是存放线性表的存储空间的长度(一般是不变的),不过语言可以动态增加容量,会带来性能损耗;
    线性表长度是数据元素的个数;
    线性表是从1开始数的,对应数组0的位置
  1.2 获取元素、插入元素、删除元素(代码中展示)

  1.3 顺序结构优缺点:
    优点:无须为表示表中元素之间的逻辑关系而增加额外的存储空间;可以快速地存取表中任一位置元素
    缺点:插入和删除操作需要移动大量的元素;当线性表长度裱花较大时,难以确定存储空间容量;造成存储空间'碎片'

<span style="color: #008000;">    //</span><span style="color: #008000;">用一维数组模拟线性表</span>    <span style="color: #0000ff;">class</span><span style="color: #000000;"> Sequential_Structure    {        </span><span style="color: #008000;">//</span><span style="color: #008000;">线性表的长度</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$num</span> = 0<span style="color: #000000;">;        </span><span style="color: #008000;">//</span><span style="color: #008000;">数组长度</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$len</span> = 0<span style="color: #000000;">;        </span><span style="color: #008000;">//</span><span style="color: #008000;">数组模拟</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 初始化结构          * @param Int $len 最大数组长度          * @param Array $arr 数组          * @return           </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$len</span>, <span style="color: #0000ff;">Array</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">)        {            </span><span style="color: #800080;">$this</span>->len = <span style="color: #800080;">$len</span><span style="color: #000000;">;            </span><span style="color: #800080;">$length</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$length</span> > 0 && <span style="color: #800080;">$length</span> $len<span style="color: #000000;">)            {                </span><span style="color: #800080;">$this</span>->arr = <span style="color: #800080;">$arr</span><span style="color: #000000;">;                </span><span style="color: #800080;">$this</span>->num = <span style="color: #800080;">$length</span><span style="color: #000000;">;            }        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取线性表元素          * @param Int $i 需要获取的第几个元素          * @return           </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> get_elem(<span style="color: #800080;">$i</span><span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->num == 0 || <span style="color: #800080;">$i</span> $i > <span style="color: #800080;">$this</span>->num) <span style="color: #008000;">//</span><span style="color: #008000;">判断查找是否合理</span>                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>-1];    <span style="color: #008000;">//</span><span style="color: #008000;">返回数据,时间复杂度O(1)</span><span style="color: #000000;">        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 插入元素(顺序结构中,插入元素后,后面所有的数据都要后移,平均时间复杂度O(1)):          * 如果插入位置不合理,失败          * 如果线性长度大于数组长度,则返回错误或者动态增加容量          * 从最后一个元素开始向前遍历到第i个位置,分别将它们向后移动一个位置          * 将元素插入i位置          * @param Int $i 需要插入到第几个元素          * @param Int $elem 插入的节点          * @return bool          </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> insert_elem(<span style="color: #800080;">$i</span>,  <span style="color: #800080;">$elem</span><span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->num == <span style="color: #800080;">$this</span>->len) <span style="color: #008000;">//</span><span style="color: #008000;">顺序线性表已满</span>                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $i > (<span style="color: #800080;">$this</span>->num+1)) <span style="color: #008000;">//</span><span style="color: #008000;">i不在范围之内</span>                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$i</span> $this->num)  <span style="color: #008000;">//</span><span style="color: #008000;">若数据插入位置不在表尾</span><span style="color: #000000;">            {                </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$k</span> = <span style="color: #800080;">$this</span>->num-1; <span style="color: #800080;">$k</span> >= <span style="color: #800080;">$i</span>-1; --<span style="color: #800080;">$k</span>) <span style="color: #008000;">//</span><span style="color: #008000;">后面所有元素往后移动一位</span>                    <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$k</span>+1] = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$k</span><span style="color: #000000;">];            }            </span><span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>-1] = <span style="color: #800080;">$elem</span>; <span style="color: #008000;">//</span><span style="color: #008000;">插入元素</span>            ++<span style="color: #800080;">$this</span>-><span style="color: #000000;">num;            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 删除元素(顺序结构中,插入元素后,后面所有的数据都要前移,平均时间复杂度O(1)):          * 如果删除位置不合理,失败          * 将元素删除          * 从最后删除元素开始向后遍历到最后,分别将它们向前移动一个位置          * @param Int $i 需要仓储的第几个元素          * @return bool          </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> delete_elem(<span style="color: #800080;">$i</span><span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->num == 0) <span style="color: #008000;">//</span><span style="color: #008000;">线性表为空</span>                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $i > <span style="color: #800080;">$this</span>->num) <span style="color: #008000;">//</span><span style="color: #008000;">删除位置不正确</span>                <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $this->num) <span style="color: #008000;">//</span><span style="color: #008000;">删除位置不是表尾</span><span style="color: #000000;">            {                </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$k</span> = <span style="color: #800080;">$i</span>; <span style="color: #800080;">$k</span> $this->num; ++<span style="color: #800080;">$k</span>) <span style="color: #008000;">//</span><span style="color: #008000;">前移</span>                    <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$k</span>-1] = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$k</span><span style="color: #000000;">];            }                </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$this</span>->num-1<span style="color: #000000;">]);            </span>--<span style="color: #800080;">$this</span>-><span style="color: #000000;">num;            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取顺序表          * @return           </span><span style="color: #008000;">*/</span>            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_arr()        {            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">arr;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取长度          * @return           </span><span style="color: #008000;">*/</span>            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_len()        {           </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>('num' => <span style="color: #800080;">$this</span>->num , 'len' => <span style="color: #800080;">$this</span>-><span style="color: #000000;">len);        }    }        </span><span style="color: #800080;">$link</span> = <span style="color: #0000ff;">new</span> Sequential_Structure(10,[1,4,8,7<span style="color: #000000;">]);    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$link</span>->get_elem(2<span style="color: #000000;">);    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->insert_elem(5,5<span style="color: #000000;">));    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>-><span style="color: #000000;">get_arr());    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>-><span style="color: #000000;">get_len());    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->delete_elem(1<span style="color: #000000;">));    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>-><span style="color: #000000;">get_arr());    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->get_len());
로그인 후 복사
<span style="color: #0000ff;">输出:<br>boolean</span> <span style="color: #0000ff;">true</span><span style="color: #0000ff;">array</span> (size=5<span style="color: #000000;">)  </span>0 => int 1  1 => int 4  2 => int 8  3 => int 7  4 => int 5<span style="color: #0000ff;">array</span> (size=2<span style="color: #000000;">)  </span>'num' => int 5  'len' => int 10<span style="color: #0000ff;">boolean</span> <span style="color: #0000ff;">true</span><span style="color: #0000ff;">array</span> (size=4<span style="color: #000000;">)  </span>0 => int 4  1 => int 8  2 => int 7  3 => int 5<span style="color: #0000ff;">array</span> (size=2<span style="color: #000000;">)  </span>'num' => int 4  'len' => int 10
로그인 후 복사

 

 

二 链表存储结构(n个节点链结成一个链表)
  2.1 单链表(用数组模拟)
    2.1.1 链表中第一个结点的存储位置为头指针(通常为了方便对链表进行操作,会在单链表的第一个结点前附设一个头结点)
      注 头指针:指向链表第一个结点的指针,若链表有头结点,这是指向头结点的指针;无论链表是否为空,头指针不为空
        头结点:放在第一元素的结点之前

<span style="color: #008000;">/*</span><span style="color: #008000;">*      *    用一维数组模拟线性表      * array('data'=>data,'cur'=>cur) data为存放数据,cur为下个数组元素下标      </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">class</span><span style="color: #000000;"> Simple_Link    {        </span><span style="color: #008000;">//</span><span style="color: #008000;">数组长度</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$len</span> = 0<span style="color: #000000;">;        </span><span style="color: #008000;">//</span><span style="color: #008000;">数组模拟</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();        </span><span style="color: #008000;">//</span><span style="color: #008000;">数组中空闲的下标</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$space_arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 初始化结构          * @param Int $len 最大数组长度          * @param Array $arr 数组          * @return           </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$len</span>, <span style="color: #0000ff;">Array</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">)        {            </span><span style="color: #800080;">$this</span>->len = <span style="color: #800080;">$len</span><span style="color: #000000;">;            </span><span style="color: #800080;">$length</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);            </span><span style="color: #800080;">$this</span>->arr[0]['data'] = <span style="color: #800080;">$length</span><span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->arr[0]['cur'] = 0<span style="color: #000000;">;            </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $length; ++<span style="color: #800080;">$i</span><span style="color: #000000;">)                </span><span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>]['cur'] = <span style="color: #800080;">$i</span>+1;  <span style="color: #008000;">//</span><span style="color: #008000;">模拟链表的指向</span>                        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$length</span><span style="color: #000000;">)                </span><span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$length</span>]['cur'] = 0;  <span style="color: #008000;">//</span><span style="color: #008000;">最后一个结点指针空</span>                        <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = <span style="color: #800080;">$length</span> + 1; <span style="color: #800080;">$i</span> $len-<span style="color: #800080;">$length</span> ; ++<span style="color: #800080;">$i</span>) <span style="color: #008000;">//</span><span style="color: #008000;">空闲数组</span>                <span style="color: #008080;">array_unshift</span>(<span style="color: #800080;">$this</span>->space_arr,<span style="color: #800080;">$i</span><span style="color: #000000;">);          }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取线性表元素:          * 初始化$j从1开始          * 当$j<span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> get_elem(<span style="color: #800080;">$i</span><span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $i</span> > <span style="color: #800080;">$this</span>->arr[0]['data'<span style="color: #000000;">])                 </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #800080;">$j</span> = 1<span style="color: #000000;">;            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$this</span>->arr[0]['cur'];  <span style="color: #008000;">//</span><span style="color: #008000;">指向第一个结点</span>            <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$j</span> $i<span style="color: #000000;">)            {                </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$cur</span>]['cur'<span style="color: #000000;">];                </span>++<span style="color: #800080;">$j</span><span style="color: #000000;">;            }                    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$cur</span>]['data'<span style="color: #000000;">];        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 插入元素:          * 初始化$j从1开始          * 当$j<span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> insert_elem(<span style="color: #800080;">$i</span>, <span style="color: #800080;">$elem</span><span style="color: #000000;">)        {            </span><span style="color: #800080;">$len</span> = <span style="color: #800080;">$this</span>->arr[0]['data'] + 1<span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $i</span> > <span style="color: #800080;">$len</span><span style="color: #000000;">)                 </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #800080;">$j</span> = <span style="color: #800080;">$this</span>->malloc(); <span style="color: #008000;">//</span><span style="color: #008000;">获取空闲下标</span>            <span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$j</span><span style="color: #000000;">)                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$j</span>]['data'] = <span style="color: #800080;">$elem</span><span style="color: #000000;">;                        </span><span style="color: #800080;">$k</span> = 1<span style="color: #000000;">;            </span><span style="color: #800080;">$index</span> = 0<span style="color: #000000;">;            </span><span style="color: #800080;">$cur</span> = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->arr[0]['cur']) ? <span style="color: #800080;">$this</span>->arr[0]['cur'] : 0;  <span style="color: #008000;">//</span><span style="color: #008000;">指向第一个结点</span>            <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$k</span> $i<span style="color: #000000;">)            {                </span><span style="color: #008000;">//</span><span style="color: #008000;">记录当前cur和下一个cur</span>                <span style="color: #800080;">$index</span> = <span style="color: #800080;">$cur</span><span style="color: #000000;">;                  </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$index</span>]['cur'<span style="color: #000000;">];                </span>++<span style="color: #800080;">$k</span><span style="color: #000000;">;            }            </span><span style="color: #008000;">//</span><span style="color: #008000;">改变指针指向</span>            <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$index</span>]['cur'] = <span style="color: #800080;">$j</span><span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$j</span>]['cur'] = <span style="color: #800080;">$cur</span><span style="color: #000000;">;            </span>++<span style="color: #800080;">$this</span>->arr[0]['data'<span style="color: #000000;">];            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 删除元素:          * 初始化$j从1开始          * 当$j<span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> delete_elem(<span style="color: #800080;">$i</span><span style="color: #000000;">)        {            </span><span style="color: #800080;">$len</span> = <span style="color: #800080;">$this</span>->arr[0]['data'<span style="color: #000000;">];            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span> $i</span> > <span style="color: #800080;">$len</span><span style="color: #000000;">)                 </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;                        </span><span style="color: #800080;">$k</span> = 1<span style="color: #000000;">;            </span><span style="color: #800080;">$index</span> = 0<span style="color: #000000;">;             </span><span style="color: #800080;">$cur</span> = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->arr[0]['cur']) ? <span style="color: #800080;">$this</span>->arr[0]['cur'] : 0;  <span style="color: #008000;">//</span><span style="color: #008000;">指向第一个结点</span>            <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$k</span> $i<span style="color: #000000;">)            {                </span><span style="color: #008000;">//</span><span style="color: #008000;">记录当前cur和下一个cur</span>                <span style="color: #800080;">$index</span> = <span style="color: #800080;">$cur</span><span style="color: #000000;">;                  </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$index</span>]['cur'<span style="color: #000000;">];                </span>++<span style="color: #800080;">$k</span><span style="color: #000000;">;            }            </span><span style="color: #008000;">//</span><span style="color: #008000;">改变指针指向</span>            <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$index</span>]['cur'] = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$cur</span>]['cur'<span style="color: #000000;">];                    </span><span style="color: #800080;">$this</span>->free(<span style="color: #800080;">$cur</span><span style="color: #000000;">);            </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$cur</span><span style="color: #000000;">]);            </span>--<span style="color: #800080;">$this</span>->arr[0]['data'<span style="color: #000000;">];            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取空闲的结点下标,也就是相当于申请一个空结点          * @return           </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> malloc()        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">space_arr))                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_pop</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">space_arr);        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 释放结点          * @param Int $cur 需要回收的结点下标          </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> free(<span style="color: #800080;">$cur</span><span style="color: #000000;">)        {            </span><span style="color: #008080;">array_push</span>(<span style="color: #800080;">$this</span>->space_arr, <span style="color: #800080;">$cur</span><span style="color: #000000;">);        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 打印          * @return           </span><span style="color: #008000;">*/</span>            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> print_arr()        {            </span><span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->arr[0]['data'<span style="color: #000000;">]))            {    </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>]['cur'<span style="color: #000000;">])                {                    </span><span style="color: #800080;">$i</span> = <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>]['cur'<span style="color: #000000;">];                    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>->arr[<span style="color: #800080;">$i</span>]['data'].' '<span style="color: #000000;">;                }            }        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 获取长度          * @return           </span><span style="color: #008000;">*/</span>            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_len()        {           </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>('num' => <span style="color: #800080;">$this</span>->arr[0]['data'] , 'len' => <span style="color: #800080;">$this</span>-><span style="color: #000000;">len);        }    }    </span><span style="color: #800080;">$link</span> = <span style="color: #0000ff;">new</span> Simple_Link(10,<span style="color: #0000ff;">array</span><span style="color: #000000;">());    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->insert_elem(1,5<span style="color: #000000;">));    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->insert_elem(2,4<span style="color: #000000;">));    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->insert_elem(1,6<span style="color: #000000;">));    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>->delete_elem(3<span style="color: #000000;">));    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$link</span>-><span style="color: #000000;">print_arr();    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$link</span>-><span style="color: #000000;">get_len());                输出:        </span><span style="color: #0000ff;">boolean</span> <span style="color: #0000ff;">true</span>        <span style="color: #0000ff;">boolean</span> <span style="color: #0000ff;">true</span>        <span style="color: #0000ff;">boolean</span> <span style="color: #0000ff;">true</span>        <span style="color: #0000ff;">boolean</span> <span style="color: #0000ff;">true</span>        6 5        <span style="color: #0000ff;">array</span> (size=2<span style="color: #000000;">)          </span>'num' => int 2          'len' => int 10           
로그인 후 복사

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿