Php redeem single-chain table (static linked list)-php Tutorial

Source: Internet
Author: User
Php implements a single-chain table (static linked list) & lt ;? Php ** single-chain table PHP implementation ** @ authorzhaojiangwei * @ since20111020 * node class classNode {private $ nextNULL; next node pointer php implementation single-chain table (static linked list)
 Data = $ data; $ next & $ this-> next = $ next;} public function getData () {return $ this-> data;} public function setData ($ data) {$ this-> data = $ data;} public function getNext () {return $ this-> next;} public function setNext ($ next) {$ this-> next = $ next; }}// single-chain table class LinkList {private $ data_list = NULL; // public function LinkList ($ data = false) of the node set) {$ this-> data_list = array (); $ title = new Node (NU LL); $ this-> data_list [] = $ title; if ($ data) {if (is_array ($ data) {$ this-> addMoreData ($ data );} else {$ this-> addData ($ data) ;}}// return the value of the nth node public function getNodeByNumber ($ number) {return $ this-> data_list [$ this-> findKeyByNumber ($ number)]-> getData () ;}// add a group of nodes public function addMoreData ($ datas) {foreach ($ datas as $ value) {$ this-> addData ($ value) ;}// add a unified node entry, for external calls // $ number is added to the public behind the nodes Function addData ($ data, $ number = false) {$ node = new Node ($ data ); if ($ number = FALSE | $ number = count ($ this-> data_list) {$ this-> insertLastNode ($ node );} elseif ($ number> count ($ this-> data_list) {return false;} else {$ this-> insertNode ($ node, $ number );}} // Insert a node to the final private function insertLastNode ($ node) {$ node-> setNext (NULL); $ lastKey = $ this-> findLastNode (); $ insert_key = $ this-> insertNo Demo-array ($ node); $ this-> data_list [$ lastKey]-> setNext ($ insert_key);} // Insert a node private function insertNode ($ node, $ number) {$ insert_number = $ this-> findKeyByNumber ($ number); $ node-> setNext ($ this-> data_list [$ insert_number]-> getNext ()); $ insert_key = $ this-> insertnode1_array ($ node); $ this-> data_list [$ insert_number]-> setNext ($ insert_key );} // find the array key private function findKeyByNumber ($ numb Er) {$ I = $ key = 0; while ($ I <$ number) {$ key = $ this-> data_list [$ key]-> getNext (); $ I ++;} return $ key;} // add the node to the array private function insertnode1_array ($ node) {$ this-> data_list [] = $ node; return $ this-> getLastKey ();} // delete the public function deleteNode ($ number) node) {if ($ number = 0 | $ number> count ($ this-> data_list) {return false ;} $ pre_key = $ this-> findKeyByNumber ($ number-1); $ key = $ this-> dat A_list [$ pre_key]-> getNext (); $ this-> data_list [$ pre_key]-> setNext ($ this-> data_list [$ key]-> getNext ()); unset ($ this-> data_list [$ key]);} // search for the private function getPreNodeKey ($ key) of the previous node of a node) {foreach ($ this-> data_list as $ k =>$ v) {if ($ v-> getNext () ==$ key) {return $ k ;}} return false;} // Print the public function getData_list () {return $ this-> data_list;} // return the last private function getLastKey () {end ($ this -> Data_list); return key ($ this-> data_list);} // determines whether a key value has a private function ifExistKey ($ key) {if (array_key_exists ($ key, $ this-> data_list) {return true;} return false;} // find the end node public function findLastNode () {foreach ($ this-> data_list as $ key => $ value) {if ($ value-> getNext () = NULL) {return $ key ;}}}?>

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.