php实现的顺序线性表示例


Posted in PHP onMay 04, 2019

本文实例讲述了php实现的顺序线性表。分享给大家供大家参考,具体如下:

<?php
/*
 * 线性顺序表 ,其是按照顺序在内存进行存储,出起始和结尾以外都是一一连接的(一般都是用一维数组的形式表现)
 *
 * GetElem: 返回线性表中第$index个数据元素
 * ListLength: 返回线性表的长度
 * LocateElem: 返回给定的数据元素在线性表中的位置
 * PriorElem: 返回指定元素的前一个元素
 * NextElem: 返回指定元素的后一个元素
 * ListInsert: 在第index的位置插入元素elem
 * ListDelete: 删除第index位置的元素elem
 */
class Sequence {
  public $seqArr;
  public $length;
  public function __construct($arr) {
    $this->seqArr = $arr;
    $this->length = count($arr);
  }
  /*
   * 返回线性表中第$index个数据元素
   */
  public function GetElem($index) {
    if (($this->length) == 0 || $index < 0 || ($index > $this->length)) {
      return "Error";
    }
    return $this->seqArr[$index - 1];
  }
  /*
   * 返回线性表的长度
   *
   */
  public function ListLength() {
    return $this->length;
  }
  /*
   * 返回给定的数据元素在线性表中的位置
   */
  public function LocateElem($elem) {
    for ($i = 0; $i < ($this->length); $i++) {
      if (($this->seqArr[$i]) == $elem) {
        return $i + 1;
      }
    }
  }
  /*
   * PriorElem: 返回指定元素的前一个元素
   */
  public function PriorElem($elem) {
    for ($i = 0; $i < ($this->length); $i++) {
      if (($this->seqArr[$i]) == $elem) {
        if ($i == 0) {
          return "Error (is null) ";
        } else {
          return $this->seqArr[$i - 1];
        }
      }
    }
  }
  /*
   * NextElem: 返回指定元素的后一个元素
   */
  public function NextElem($elem) {
    for ($i = 0; $i < ($this->length); $i++) {
      if (($this->seqArr[$i]) == $elem) {
        return $this->seqArr[$i + 1];
      }
    }
  }
  /*
   * ListInsert: 在第index的位置插入元素elem
   */
  public function ListInsert($index, $elem) {
    if (($this->length) == 0 || $index < 0 || $index > ($this->length)) {
      return "Error";
    }
    for ($i = $index; $i < ($this->length); $i++) {
      $this->seqArr[$i + 1] = $this->seqArr[$i];
    }
    $this->seqArr[$index] = $elem;
    $this->length = $this->length + 1;
    return $this->seqArr;
  }
  /*
   * ListDelete: 删除第index位置的元素
   */
  public function ListDelete($index) {
    if (($this->length) == 0 || $index < 0 || $index > ($this->length - 1)) {
      return "Error";
    }
    unset($this->seqArr[$index]);
    $this->length--;
    return $this->seqArr;
  }
}
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
vBulletin Forum 2.3.xx SQL Injection
Oct 09 PHP
随机头像PHP版
Oct 09 PHP
Windows2003 下 MySQL 数据库每天自动备份
Dec 21 PHP
一周让你学会PHP 不错的学习资料
Feb 06 PHP
PHP 数组和字符串互相转换实现方法
Mar 26 PHP
php用户注册页面利用js进行表单验证具体实例
Oct 17 PHP
PHP定时任务延缓执行的实现
Oct 08 PHP
PHPExcel读取EXCEL中的图片并保存到本地的方法
Feb 14 PHP
phpcms手机内容页面添加上一篇和下一篇
Jun 05 PHP
php使用glob函数遍历文件和目录详解
Sep 23 PHP
yii2实现分页,带搜索的分页功能示例
Jan 07 PHP
PHP中的函数声明与使用详解
May 27 PHP
TP5.0框架实现无限极回复功能的方法分析
May 04 #PHP
PHP7 mongoDB扩展使用的方法分享
May 02 #PHP
PHP超低内存遍历目录文件和读取超大文件的方法
May 01 #PHP
Yii框架学习笔记之session与cookie简单操作示例
Apr 30 #PHP
YII框架学习笔记之命名空间、操作响应与视图操作示例
Apr 30 #PHP
YII框架关联查询操作示例
Apr 29 #PHP
YII框架页面缓存操作示例
Apr 29 #PHP
You might like
php正则表达匹配中文问题分析小结
2012/03/25 PHP
PHP扩展模块memcached长连接使用方法分析
2014/12/24 PHP
php实现简单四则运算器
2020/11/29 PHP
JavaScript 继承详解(一)
2009/07/13 Javascript
JavaScript中的replace()方法使用详解
2015/06/06 Javascript
有关json_decode乱码及NULL的问题
2015/10/13 Javascript
AngularJS 获取ng-repeat动态生成的ng-model值实例详解
2016/11/29 Javascript
vue router路由嵌套不显示问题的解决方法
2017/06/17 Javascript
基于JS实现仿京东搜索栏随滑动透明度渐变效果
2017/07/10 Javascript
vue组件间通信六种方式(总结篇)
2019/05/15 Javascript
原生jQuery实现只显示年份下拉框
2020/12/24 jQuery
[04:29]2014DOTA2国际邀请赛 主赛事第三日TOPPLAY
2014/07/21 DOTA
python 图片验证码代码分享
2012/07/04 Python
python设置windows桌面壁纸的实现代码
2013/01/28 Python
Python中类的继承代码实例
2014/10/28 Python
给Python中的MySQLdb模块添加超时功能的教程
2015/05/05 Python
Python内置函数 next的具体使用方法
2017/11/24 Python
Python subprocess模块功能与常见用法实例详解
2018/06/28 Python
python实现删除列表中某个元素的3种方法
2020/01/15 Python
Python 安装 virturalenv 虚拟环境的教程详解
2020/02/21 Python
享誉全球的多元化时尚精品购物平台:Farfetch发发奇(支持中文)
2017/08/08 全球购物
Myprotein台湾官方网站:全球领先的运动营养品牌
2018/12/10 全球购物
世界上最全面的草药补充剂和顶级品牌维生素网站:HerbsPro
2019/01/20 全球购物
大码女装:Ulla Popken
2019/08/06 全球购物
国际贸易毕业生求职信范文
2014/02/21 职场文书
小学捐书活动总结
2014/07/05 职场文书
学校工作推荐信范文
2014/07/11 职场文书
敲诈同学钱财检讨书范文
2014/11/18 职场文书
毕业生对母校寄语
2015/02/26 职场文书
研究生简历自我评
2015/03/11 职场文书
2019新员工试用期转正工作总结范文
2019/08/21 职场文书
导游词之湖州-太湖
2019/10/11 职场文书
matplotlib之pyplot模块实现添加子图subplot的使用
2021/04/25 Python
如何理解Vue简单状态管理之store模式
2021/05/15 Vue.js
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js
Android开发 使用文件储存的方式保存QQ密码
2022/04/24 Java/Android