PHP Data Structure 2: PHP implementation of sequence tables in linear tables

Source: Internet
Author: User
Linear table (LinearList): is composed of n (n limit 0) data elements (nodes) a1, a2 ,... An finite sequence. All nodes in the sequence have the same data type. N is the length of a linear table.

Linear table

(1) basic features: the most basic, simple, and commonly used data structure in this structure:

1. there is a unique data element called "first;

2. there is a unique data element called "last;

3. except the first element, each element has a unique direct precursor;

4. except for the last element, each element has a unique direct successor.

(2) definition:

Linear table (Linear List): is composed of n (n limit 0) data elements (nodes) a1, a2 ,... An finite sequence. All nodes in the sequence have the same data type. N is the length of a linear table.

When n = 0, it is called an empty table.

When n> 0, the non-empty linear table is recorded as: (a1, a2 ,... An)
A1 is the first (first) node of a linear table, and an is the last (last) node of a linear table. (3) sequential storage of linear tables: nodes of linear tables are stored in a group of sequential storage units in a logical order. A linear table stored in this way is called a sequential table.

Features of a linear table stored in sequence:

1. the logical sequence of a linear table is the same as that of a physical table;

2. the relationship between data elements is reflected by the "physical location adjacent" of the elements in the computer.

PHP implementation of sequence tables in linear tables

 

 Oll = $ oll;}/*** clear sequence table * @ return void */public function _ destruct () {foreach ($ this-> oll as $ key = >$ value) {unset ($ this-> oll [$ key]);} /*** determine whether the sequence table is EMPTY * @ return boolean returns true if it is null; otherwise, false is returned. **/public function isEmpty () {if (count ($ this-> oll)> 0) {return false;} else {return true ;}} /*** return the length of the sequence table * @ return int **/public function getLength () {return count ($ this-> oll );} /*** return the element whose subscript is $ key in the sequence table ** @ param mixed $ Key: subscript * @ return mixed **/public function getElement ($ key) {return $ this-> oll [$ key];} /*** return the position of an element in the sequence table ** @ param mixed $ value the value of an element in the sequence table * @ return int starts from 1, if-1 is returned, this element does not exist. **/public function getElementPosition ($ value) {$ I = 0; foreach ($ this-> oll as $ val) {$ I ++; if (strcmp ($ value, $ val) === 0) {return $ I ;}} return-1 ;} /*** return the direct precursor element of an element in the sequence table ** @ param mixed $ value the value of an element in the sequence table * @ param bool $ tag if $ If the value is the lower mark, it is 1. if $ value is the element value, it is 2 * @ return array ('value' => ...) directly precursor element value, array ('key' => ...) direct precursor element subscript **/public function getElementPredecessorr ($ value, $ tag = 1) {$ I = 0; foreach ($ this-> oll as $ key => $ val) {$ I ++; if ($ tag = 1) {if (strcmp ($ key, $ value) = 0) {if ($ I = 1) {return false;} prev ($ this-> oll); return array ('value' => current ($ this-> oll ), 'key' => key ($ this-> oll) ;}} if ($ tag = 2) {if (strcmp ($ val, $ v Alue) === 0) {if ($ I = 1) {return false;} prev ($ this-> oll ); return array ('value' => current ($ this-> oll), 'key' => key ($ this-> oll ));}}}} /*** return the direct successor element of an element ** @ param mixed $ value the value of an element in the sequence table * @ param bool $ tag: 1 if $ value is the lower mark, if $ value is an element value, it is 2 * @ return array ('value' => ...) directly successor element value, array ('key' => ...) directly subscripts to the successor element **/public function getElementSubsequence ($ value, $ tag = 1) {$ I = 0; $ len = count ($ this-> oll); fo Reach ($ this-> oll as $ key => $ val) {$ I ++; if ($ tag = 1) {if (strcmp ($ key, $ value) = 0) {if ($ I = $ len) {return false;} return array ('value' => current ($ this-> oll ), 'key' => key ($ this-> oll) ;}} if ($ tag = 2) {if (strcmp ($ val, $ value) = 0) {if ($ I ==$ len) {return false;} return array ('value' => current ($ this-> oll ), 'key' => key ($ this-> oll) ;}} return false ;} /*** insert a new node at the specified position ** @ param mixed $ p new node insertion position, starting from 1 * @ param mixed $ Value the value of the new node in the ordered table * @ param mixed $ subscript of the new node in the key ordered table * @ param bool $ tag specifies whether the subscript of the new node is used. 1 indicates the Default subscript, 2 indicates that the specified subscript * @ return bool returns true if insertion is successful, and false if insertion fails. **/public function getInsertElement ($ p, $ value, $ key = null, $ tag = 1) {$ p = (int) $ p; $ len = count ($ this-> oll); $ oll = array (); $ I = 0; if ($ p> $ len | $ p <1) {return false;} foreach ($ this-> oll as $ k =>$ v) {$ I ++; if ($ I = (int) $ p) {if ($ tag = 1) {$ oll [] = $ value;} else if ($ tag = 2) {$ keys = array_keys ($ Oll); $ j = 0; if (is_int ($ key) {while (in_array ($ key, $ keys, true) {$ key ++ ;}} else {while (in_array ($ key, $ keys, true) {$ j ++; $ key. = (string) $ j ;}$ oll [$ key] = $ value ;}else {return false ;}$ key = $ k; $ j = 0; $ keys = array_keys ($ oll); if (is_int ($ key) {$ oll [] = $ v;} else {while (in_array ($ key, $ keys, true) {$ j ++; $ key. = (string) $ j ;}$ oll [$ key] = $ v ;}} else {if ($ I> $ p) {$ key = $ k; $ j = 0; $ keys = array_keys ($ oll); if (is_int ($ key) {$ oll [] = $ v;} else {while (In_array ($ key, $ keys, true) {$ j ++; $ key. = (string) $ j ;}$ oll [$ key] = $ v ;}} else {if (is_int ($ k) {$ oll [] = $ v ;} else {$ oll [$ k] = $ v ;}}$ this-> oll = $ oll; return true ;} /*** return an element in the sequence table based on the element position ** @ param int $ position the element position starts from 1 * @ return array ('value' => ...) element value, array ('key' => ...) element subscript */public function getElemForPos ($ position) {$ I = 0; $ len = count ($ this-> oll); $ position = (int) $ position; if ($ position> $ len | $ position <1) {Return false;} foreach ($ this-> oll as $ val) {$ I ++; if ($ I = $ position) {return array ('value' => current ($ this-> oll), 'key' => key ($ this-> oll ));}}} /*** delete an element in the sequence table based on the subscript or element value ** @ param mixed $ value element subscript or value * @ param int $ tag 1 indicates that $ value is a subscript, 2 indicates that $ value is the element value * @ return bool. true is returned for success, and false is returned for failure. **/public function getDeleteElement ($ value, $ tag = 1) {$ len = count ($ this-> oll); foreach ($ this-> oll as $ k => $ v) {if ($ tag = 1) {if (st Rcmp ($ k, $ value) === 0) {} else {if (is_int ($ k) {$ oll [] = $ v ;} else {$ oll [$ k] = $ v ;}} if ($ tag = 2) {if (strcmp ($ v, $ value) === 0) {} else {if (is_int ($ k) {$ oll [] = $ v;} else {$ oll [$ k] = $ v ;}}}} $ this-> oll = $ oll; if (count ($ this-> oll) ==$ len) {return false;} return true ;} /*** delete an element in the sequence table according to the element position ** @ param int $ position the element position starts from 1 * @ return bool success returns true, returns false if an error occurs. **/public function getDeleteEleForPos ($ position) {$ len = count ($ thi S-> oll); $ I = 0; $ position = (int) $ position; if ($ position> $ len | $ position <1) {return false ;} foreach ($ this-> oll as $ k => $ v) {$ I ++; if ($ I = $ position) {} else {if (is_int ($ k) {$ oll [] = $ v;} else {$ oll [$ k] = $ v ;}}} $ this-> oll = $ oll; if (count ($ this-> oll) ==$ len) {return false;} return true ;}}?> $ Oll = new OrderLinearList (array ('Xu '=> 'Xu dianyang', 32, "right", 'dianyang '=> 10,455 )); // Determine whether the sequence table is empty. if the returned value is false, the var_dump ($ oll-> isEmpty () is not empty. echo"
"; // Return the length of the sequence table. 6 echo $ oll-> getLength (); echo" is returned"
"; // Returns an element var_dump ($ oll-> getElement (1); echo"
"; // Return the location of an element in the sequence table echo $ oll-> getElementPosition (" right "); echo"
"; // Return the direct precursor element var_dump ($ oll-> getElementPredecessorr (" right ", 2) of an element in the sequence table; echo"
"; // Return the directly successor element var_dump ($ oll-> getElementSubsequence (" right ", 2) of an element in the sequence table; echo"
"; // Return an element var_dump ($ oll-> getElemForPos (2) in the sequence table based on the element location; echo"
"; // Delete an element var_dump ($ oll-> getDeleteElement ('Xu dianyang ', $ tag = 2) in the sequence table based on the subscript or element value); echo"
"; // Delete an element var_dump ($ oll-> getDeleteEleForPos (1) in the sequence table based on the element location; echo"
"; $ Oll-> getInsertElement (3," Xu Zhen ", $ key =" xuzheng ", $ tag = 2); var_dump ($ oll-> oll); 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.