Php code for sequential storage of linear tables (add, delete, query, and modify)

Source: Internet
Author: User
Php code for sequential storage of linear tables. For more information, see The code is as follows:
/*
* File name: linearList. php
* Function: sequential storage of linear tables with data structures
* Author: Li Jinhuan
* @ Copyright: www.drw1314.com
*/
Class linearList {
Private $ arr;
Private $ length;
Const MAXSIZE = 100;
/*
* Constructor: checks whether the empty table is a flying empty table and instantiates the table.
* @ Param array $ arr input array
* @ Param int $ n length of the input array
* @ Ruturn void;
*/
Function _ construct ($ arr, $ n ){
If ($ n> self: MAXSIZE ){
Echo 'Sorry, the length of the array '. $ n.' exceeds the memory space! '. Self: MAXSIZE;
} Else if ($ n <0 ){
Echo 'exception. the length cannot be negative. ';
} Else if ($ n = 0 ){
Echo'
... You have created an empty table with an array length of 0 ....

';
$ This-> arr = $ arr;
$ This-> length = $ n;
} Else {
Echo'
... A table is created successfully ....

';
$ This-> arr = $ arr;
$ This-> length = $ n;
}
}
/*
* Searches by bit and returns the searched value.
* @ Ruturn string;
* @ Param int $ n locate
*/
Function findValue ($ n ){
If ($ n> $ this-> length | $ n <1 ){
Return 'input location '. $ n.' is incorrect. please be within the range of 1 to '. $ this-> length ';
}
Return 'the value of the nth. $ n. 'You are looking for is'. $ this-> arr [$ n-1];
}
/*
* Search by value and return the location found
* @ Ruturn string;
* @ Param int $ n query value
*/
Function findSite ($ n ){
For ($ I = 0; $ I <$ this-> length; $ I ++ ){
If ($ this-> arr [$ I] = $ n ){
$ B = $ I + 1;
Return 'the value you are looking for '. $ n.' corresponds to '. $ B;
} Else {
$ V = false;
}
}
If (! $ V ){
Return 'value you are looking for '. $ n.' doesn't exist ';
}
}
/*
* Insert a value at the selected position
* @ Ruturn array;
* @ Param int $ I insert position
* @ Param int $ v insert value
*/
Function insertValue ($ I, $ v ){
If ($ I <1 | $ I> self: MAXSIZE ){
Echo 'insertion location '. $ I.' is incorrect. please be in the range from 1 to '. self: MAXSIZE ';
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;
}
/*
* Delete a value at a specified position.
* @ Ruturn array;
* @ Param int $ I position
*/
Function deleteValue ($ I ){
If ($ I <1 | $ I> $ this-> length ){
Echo 'Selected location '. $ I.' is incorrect. please be within the range of 1 to '. $ this-> length ';
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'
... Destroy an empty table...
';
} Else {
Echo'
... A table is successfully destroyed ..
';
}
}
}
// The following is a use case
$ Arr = array (10,125,123 );
$ N = 5;
$ LinearList = new linearList ($ arr, $ n );
Echo $ linearList-> findValue (5 ).'
';
Echo $ linearList-> findSite (4 ).'
';
Echo'
'; 
print_r($linearList->insertValue(20,300));
echo '
';
Echo'
'; 
print_r($linearList->deleteValue(1));
echo '
';

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.