Sequential linear representation example of php implementation

  • 2021-12-09 08:39:20
  • OfStack

In this paper, an example is given to describe the sequential linear table implemented by php. Share it for your reference, as follows:


<?php
/*
 *  Linear sequence table  , It is stored in memory in sequence , Except for the beginning and the end 11 Connected (1 Is generally used 1 Formal representation of dimensional array )
 *
 * GetElem:  Returns the first in the linear table $index Data elements 
 * ListLength:  Returns the length of the linear table 
 * LocateElem:  Returns the position of a given data element in the linear table 
 * PriorElem:  Returns the preceding of the specified element 1 Elements 
 * NextElem:  Returns the after of the specified element 1 Elements 
 * ListInsert:  In the first index Insert an element at the position of elem
 * ListDelete:  Delete the index Element of location elem
 */
class Sequence {
  public $seqArr;
  public $length;
  public function __construct($arr) {
    $this->seqArr = $arr;
    $this->length = count($arr);
  }
  /*
   *  Returns the first in the linear table $index Data elements 
   */
  public function GetElem($index) {
    if (($this->length) == 0 || $index < 0 || ($index > $this->length)) {
      return "Error";
    }
    return $this->seqArr[$index - 1];
  }
  /*
   *  Returns the length of the linear table 
   *
   */
  public function ListLength() {
    return $this->length;
  }
  /*
   *  Returns the position of a given data element in the linear table 
   */
  public function LocateElem($elem) {
    for ($i = 0; $i < ($this->length); $i++) {
      if (($this->seqArr[$i]) == $elem) {
        return $i + 1;
      }
    }
  }
  /*
   * PriorElem:  Returns the preceding of the specified element 1 Elements 
   */
  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:  Returns the after of the specified element 1 Elements 
   */
  public function NextElem($elem) {
    for ($i = 0; $i < ($this->length); $i++) {
      if (($this->seqArr[$i]) == $elem) {
        return $this->seqArr[$i + 1];
      }
    }
  }
  /*
   * ListInsert:  In the first index Insert an element at the position of 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:  Delete the index Element of location 
   */
  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;
  }
}
?>

More readers interested in PHP can check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: