php实现顺序线性表-php教程

资源魔 45 0

甚么是线性程序表?

线性程序表是指依照程序正在内存进行存储,除了肇始以及末端之外都是逐个衔接的(普通都是用一维数组的方式体现)。

(收费学习视频教程分享: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完成程序线性表的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 顺序 线性表

抱歉,评论功能暂时关闭!