本文实例讲述了php实现的顺序线性表。分享给大家供大家参考,具体如下:
<?php/upm大学* * 线性顺序表 ,其是按照顺序在内存进行存储,出起始和结尾以外都是一一连接的(一般都是用一维数组的形式表现) * * getelem: 返回线性表中第$index个数据元素 * listlength: 返回线性表的长度 * locateelem: 返回给定的数据元素在线性表中的位置 * priorelem: 返回指定元素的前一个元素 * nextelem: 返回指定元素的后一个元素 * listinrt: 在第index的位置插入元素elem * listdelete: 手机迷作文删除第index位置的元素elem */class quence { public $qarr; public $length; public function __construct($arr) { $this->qarr = $arr; $this->length = count($arr); } /* * 返回线性表中第$index个数据元素 */ public function getelem($index) { if (($this->length) == 0 || $index < 0 || ($index > $this->length)) { return "error"; } return $this->qarr[$index - 1]; } /* * 返回线性表的长度 * */ public function listlength() { return $this->length; } /* * 返回给定的数据元素在线性表中的位置 */ public function locateel党员汇报思想em($elem) { for ($i = 0; $i < ($this->length); $i++) { if (($this->qarr[$i]) == $elem) { return $i + 1; } } } /* * priorelem: 返回指定元素的前一个元素 */ public function priorelem($elem) { for ($i = 0; $i &qq怎么下载lt; ($this->length); $i++) { if (($this->qarr[$i]) == $elem) { if ($i == 0) { return "error (is null) "; } el { return $this->qarr[$i - 1]; } } } } /* * nextelem: 返回指定元素的后一个元素 */ public function nextelem($elem) { for ($i = 0; $i < ($this->length); $i++) { if (($this->树倒猢狲散是什么意思qarr[$i]) == $elem) { return $this->qarr[$i + 1]; } } } /* * listinrt: 在第index的位置插入元素elem */ public function listinrt($index, $elem) { if (($this->length) == 0 || $index < 0 || $index > ($this->length)) { return "error"; } for ($i = $index; $i < ($this->length); $i++) { $this->qarr[$i + 1] = $this->qarr[$i]; } $this->qarr[$index] = $elem; $this->length = $this->length + 1; return $this->qarr; } /* * listdelete: 删除第index位置的元素 */ public function listdelete($index) { if (($this->length) == 0 || $index < 0 || $index > ($this->length - 1)) { return "error"; } unt($this->qarr[$index]); $this->length--; return $this->qarr; }}?>
更多关于php相关内容感兴趣的读者可查看本站专题:《php数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《php数组(array)操作技巧大全》、《php常用遍历算法与技巧总结》及《php数学运算技巧总结》
希望本文所述对大家php程序设计有所帮助。
本文发布于:2023-04-07 08:55:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3afe6483f87150247097ccb90fd16a19.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php实现的顺序线性表示例.doc
本文 PDF 下载地址:php实现的顺序线性表示例.pdf
留言与评论(共有 0 条评论) |