PHP Data Structure 3: PHP implementation of a single-chain table in a linear table

Source: Internet
Author: User
Chain Storage of linear tables: stores data elements in a linear table with any group of storage units. Linear table stored in this method is short for linear linked list

Chain Storage of linear tables: stores data elements in a linear table with any group of storage units. Linear table stored in this method is short for linear linked list.

The characteristics of chained linear table storage: any group of storage units that store nodes in the linked list can be continuous or non-continuous, or even scattered anywhere in the memory.

The logical and physical order of nodes in the linked list is not necessarily the same.

PHP single-chain table

 = 1) knots getDeleteElemForValue () * 13. delete all duplicate values in the single-chain table getElemUnique () **/header ("content-type: text/html; charset = UTF-8"); class LNode {public $ mElem; public $ mNext; public function _ construct () {$ this-> mElem = null; $ this-> mNext = null ;}} class SingleLinkedList {// header node data public $ mElem; // next node pointer public $ mNext; // The length of a single-chain table public static $ mLength = 0; /*** initialize a single-chain table ** @ return void */public function _ construct () {$ this-> mElem = null; $ th Is-> mNext = null;}/*** clear a single-chain table ** @ return void */public function clearSLL () {if (self ::$ mLength> 0) {while ($ this-> mNext! = Null) {$ q = $ this-> mNext; $ this-> mNext = null; unset ($ this-> mNext ); $ this-> mNext = $ q;} self: $ mLength = 0 ;}} /*** return the length of a single-chain table ** @ return int */public static function getLength () {return self: $ mLength ;} /*** determine whether a single-chain table is empty ** @ return bool returns true if it is null. if it is not null, false is returned */public function getIsEmpty () {if (self :: $ mLength = 0 & $ this-> mNext = null) {return true;} else {return false ;}} /*** create a table using the header insertion method ** @ param array $ sarr create data for a single-chain table * @ retur N void */public function getHeadCreateSLL ($ sarr) {$ this-> clearSLL (); if (is_array ($ sarr) {foreach ($ sarr as $ value) {$ p = new LNode (); $ p-> mElem = $ value; $ p-> mNext = $ this-> mNext; $ this-> mNext = $ p; self: $ mLength ++ ;}} else {return false ;}} /*** create a table by means of tail insertion ** @ param array $ sarr create data for a single-chain table * @ return void */public function getTailCreateSLL ($ sarr) {$ this-> clearSLL (); if (is_array ($ sarr) {$ q = $ this; foreach ($ sarr as $ value) {$ p = new LNode (); $ p-> mElem = $ value; $ p-> mNext = $ q-> mNext; $ q-> mNext = $ p; $ q = $ p; self: $ mLength ++;} else {return false;}/*** returns the order of the $ I element ** @ param int $ I element, start from 1 * @ return mixed */public function getElemForPos ($ I) {$ I = (int) $ I; if ($ I> self :: $ mLength | $ I <1) {return null;} $ j = 1; $ p = $ this-> mNext; while ($ j <$ I) {$ q = $ p-> mNext; $ p = $ q; $ j ++;} return $ p ;} /*** find whether an element with a value exists in a single-chain table * if any, return the element node; otherwise, return null ** @ param mixed $ value * @ return Mixed */public function getElemIsExist ($ value) {$ p = $ this; while ($ p-> mNext! = Null & strcmp ($ p-> mElem, $ value )! = 0) {$ p = $ p-> mNext;} if (strcmp ($ p-> mElem, $ value) === 0) {return $ p ;} else {return null ;}}/*** query whether an element with a specific value exists in a single-chain table * if any, return the order of this element, otherwise, the returned value is-1 ** @ param mixed $ value * @ return mixed */public function getElemPosition ($ value) {$ p = $ this; $ j = 0; while ($ p-> mNext! = Null & strcmp ($ p-> mElem, $ value )! = 0) {$ j ++; $ p = $ p-> mNext;} if (strcmp ($ p-> mElem, $ value) = 0) {return $ j;} else {return-1 ;}/ *** insert operation for a single-chain table ** @ param int $ I insert the order of elements, that is, where the new element is inserted. starting from 1 * @ param mixed $ e, the new element value is inserted * @ return boolean. If insertion is successful, true is returned, false */public function getInsertElem ($ I, $ e) {if ($ I> self ::$ mLength | $ I <1) {return false ;} $ j = 1; $ p = $ this; while ($ p-> mNext! = Null & $ j <$ I) {$ p = $ p-> mNext; $ j ++;} $ q = new LNode (); $ q-> mElem = $ e; $ q-> mNext = $ p-> mNext; $ p-> mNext = $ q; self: $ mLength ++; return true;}/*** traverse all elements in a single-chain table ** @ return array includes all elements in a single-chain table */public function getAllElem () {$ slldata = array (); if ($ this-> getIsEmpty () {} else {$ p = $ this-> mNext; while ($ p-> mNext! = Null) {$ slldata [] = $ p-> mElem; $ p = $ p-> mNext;} $ slldata [] = $ p-> mElem ;} return $ slldata;}/*** delete the $ I element in a single link * @ param int $ I element order * @ return boolean return true if deletion is successful, false */public function getDeleteElem ($ I) {$ I = (int) $ I; if ($ I> self ::$ mLength | $ I <1) {return false;} $ p = $ this; $ j = 1; while ($ j <$ I) {$ p = $ p-> mNext; $ j ++ ;} $ q = $ p-> mNext; $ p-> mNext = $ q-> mNext; $ q = null; unset ($ q); self: $ mLength --;} /*** delete the first $ I ($ I> = 1) knots in a single-chain table whose median is $ value **@ Param mixed: value to be searched * @ param $ I number of times of deletion, that is, the first $ I @ return void */public function getDeleteElemForValue ($ value, $ I = 1) {if ($ I> 1) {$ this-> getDeleteElemForValue ($ value, $ i-1 );} $ vp = $ this-> getElemPosition ($ value); $ this-> getDeleteElem ($ vp );} /*** delete all duplicate values of a single-chain table ** @ return void */public function getElemUnique () {if (! $ This-> getIsEmpty () {$ p = $ this; while ($ p-> mNext! = Null) {$ q = $ p-> mNext; $ ptr = $ p; while ($ q-> mNext! = Null) {if (strcmp ($ p-> mElem, $ q-> mElem) === 0) {$ ptr-> mNext = $ q-> mNext; $ q-> mNext = null; unset ($ q-> mNext); $ q = $ ptr-> mNext; self: $ mLength --;} else {$ ptr = $ q; $ q = $ q-> mNext ;}} if (strcmp ($ p-> mElem, $ q-> mElem) === 0) {$ ptr-> mNext = null; self: $ mLength -- ;}$ p = $ p-> mNext ;}}}?>

 

 

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.