<?php
/*
* The file name: linearList.php
* Function: sequential storage implementation for linear tables of data structures
* author: Li Jinhuan
* @copyright:www.drw1314.com
*/
class linearList {
private $arr;
private $length;
const MAXSIZE=100;
/*
* Constructor to determine whether an empty table is still a flyempty table, and to instantiate it
* @param array $arr Input array
* @param int $n Enter the length of the array
* @ruturn void;
*/
function __construct($arr,$n) {
if($n>self::MAXSIZE) {
echo ' Sorry, the length of the array '.$n.' Out of memory space !'.self::MAXSIZE;
} else if($n<0) {
echo ' The length cannot be negative. ';
} else if($n==0) {
echo '<br/>.... You have created 1 An empty table with an array length of 0....<br/><br/>';
$this->arr=$arr;
$this->length=$n;
}else{
echo '<br/>.... Successfully created 1 table ....<br/><br/>';
$this->arr=$arr;
$this->length=$n;
}
}
/*
* Find by bit, return the value found
* @ruturn string;
* @param int $n Search location
*/
function findValue($n) {
if($n>$this->length||$n<1){
return ' Input location '.$n.' No, please 1 to '.$this->length.' Within the scope of ';
}
return ' You're looking for someone '.$n.' A value of '.$this->arr[$n-1];
}
/*
* Find by value, and return the location found
* @ruturn string;
* @param int $n Find the value of the
*/
function findSite($n) {
for($i=0;$i<$this->length;$i++){
if($this->arr[$i]==$n){
$b=$i+1;
return ' The value you're looking for '.$n.' The corresponding position is '.$b;
}else{
$v=false;
}
}
if(!$v){
return ' The value you're looking for '.$n.' There is no ';
}
}
/*
* Inserts a value at the selected location
* @ruturn array;
* @param int $i Insert the location
* @param int $v Insert the value of the
*/
function insertValue($i,$v) {
if($i<1||$i>self::MAXSIZE){
echo ' Insertion position '.$i.' No, please 1 to '.self::MAXSIZE.' Within the scope of ';
return ;
}
for($h=$this->length;$h>=$i;$h--){
$this->arr[$h]=$this->arr[$h-1];
}
if($i>$this->length){
$this->arr[$this->length]=$v;
}else{
$this->arr[$i-1]=$v;
}
$this->length++;
return $this->arr;
}
/*
* Deletes a value at the selected location
* @ruturn array;
* @param int $i location
*/
function deleteValue($i) {
if($i<1||$i>$this->length){
echo ' Selected location '.$i.' No, please 1 to '.$this->length.' Within the scope of ';
return ;
}
for($j=$i;$j<$this->length;$j++){
$this->arr[$j-1]=$this->arr[$j];
}
unset($this->arr[$this->length-1]);
$this->length--;
return $this->arr;
}
function __destruct(){
if($this->length==0){
echo '<br/>... The destruction 1 A empty table ...<br/>';
}else{
echo '<br/>... Successful destruction 1 table ..<br/>';
}
}
}
// Here is a use case
$arr=array(10,125,123,1,4);
$n=5;
$linearList=new linearList($arr, $n);
echo $linearList->findValue(5).'<br/>';
echo $linearList->findSite(4).'<br/>';
echo '<pre>';
print_r($linearList->insertValue(20,300));
echo '</pre>';
echo '<pre>';
print_r($linearList->deleteValue(1));
echo '</pre>';