Linear table of data structure-sequential storage structure (PHP code implementation)

Source: Internet
Author: User

<?php/** * *  linear table: A finite sequence of 0 or more data elements.  *  data structure of linear tables: that is, the elements of a database are stored in a contiguous memory cell. In a high-level language, it is represented as an array.  * * 1. DestroyList:  Destroy order Linear table  * 2. ClearList:  reset linear table to null  *  3. ListEmpty:  determine if a linear table is empty  * 4. ListLength:  returns the length of a linear table  * 5.  getelem:  returns the $index data element in a linear table  * 6. LocateElem:  returns the position of the given data element in the linear table  * 7.  priorelem:  returns the previous element of the specified element  * 8. NextElem:  returns the latter element of the specified element  * 9. listinsert:   Inserts an element at index position elem * 10. listdelete:  deletes the element at index position elem * */class  seqstorelist {    public  $SQARR;    public static    $length     public function __construct ($SQARR) {          $this->sqarr= $SqArr;         self:: $length = Count ($SQARR);      }        //destruction Sequence Linear table     public   Function destroylist () {         $this->sqarr=null;         self:: $length =0;    }    // Resets the linear table to an empty     public  function clearlist () {          $this->sqarr=array ();         self:: $length = 0;     }        //Judging if the linear table is empty      Public  function listempty () {        if (self:: $length = = 0) {            return  ' Is null ';         }else{             return  ' Not nUll ';         }    }    // Returns the length of the linear Table     public function listlength () {         return self:: $length;     }    //returns the first $index data element in a linear table     public function getelem ($index) {         if (self:: $length ==0 | |   $index <1 | |   $index >self:: $length) {            return   ' ERROR ';        }         return  $this->sqarr[$index -1];    }    // Returns the position of the given data element in the linear table     public function locateelem ($elem) {         for ($i =0; $i <self:: $length; $i + +) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;     if ($this->sqarr[$i] ==  $elem) {                 break;             }        }         if ($i >=self:: $length) {             return  ' ERROR ';        }         return  $i +1;    }    //Returns the previous element of a specified element      public function priorelem ($cur _elem) {         for ($i =0; $i <self:: $length; $i + +) {             if ($this->sqarr[$i] ==  $cur _elem) {                 break;            }         }        if ($i ==0 | |   $i >=self:: $length) {            return   ' ERROR ';        }         return  $this->sqarr[$i -1];    }    //returns the next element of a specified element      public function nextelem ($cur _elem) {         for ($i =0; $i <self:: $length; $i + +) {             if ($this->sqarr[$i] ==  $cur _elem) {                 break;             }         }        if ($i >=self:: $length-1) {             return  ' ERROR ';         }        return  $this->sqarr[$i +1];     }    //inserts an element at index position elem    public function  Listinsert ($index, $elem) {        if ($index <1 | |   $index >self:: $length + 1) {             return  ' ERROR ';        }         if ($index <=self:: $length) {             for ($i =self:: $length-1; $i >= $index-1; $i-) {                  $thissqarr[$i +1]= $this->sqarr[$i];            }         }         $this sqarr[$index -1]= $elem;         self:: $length ++;         return  ' OK ';    }    //listdelete:  Delete element Elem    public function listdelete ($index) at index position {         if ($index <1 | |   $index >self:: $length + 1) {             return  ' ERROR ';        }         if ($index <self:: $length) {             for ($i = $index; $i <self:: $length; $i + +) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;        $this->sqarr[$i -1]= $this->sqarr[$i];             }        }         self:: $length--;         return  $this->sqarr[$index -1];    }}


This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1598290

Linear table of data structure-sequential storage structure (PHP code implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.