This article mainly introduces the basic operation examples for simulating linked lists and lists in PHP, including addition, deletion, query, and modification of linked lists. the descriptions are basically included in code comments. For more information, see
Simulated linked list:
<? Php/*** PHP basic operations for implementing the linked list */class linkList {/*** name * @ var string */public $ name = ''; /*** No. * @ var int */public $ id = 0;/** reference the next object */public $ next = null; /*** constructors initialize data * @ param int $ id * @ param string $ name */public function _ construct ($ id = 0, $ name = '') {$ this-> name = $ name; $ this-> id = $ id;}/*** Prepaid table */public static function echo_link_list ($ head) {$ curr = $ head; while ($ curr- > Next! = Null) {echo 'name: '. $ curr-> next-> name,' No.: '. $ curr-> next-> id; echo'
'; $ Curr = $ curr-> next ;}/ *** add a new node */public static function add ($ head, $ id, $ name) {$ curr = $ head; $ obj = new linkList ($ id, $ name); while ($ curr-> next! = Null) {// if the current ID <next ID, add it to the center and add the node to the specified sequence position if ($ curr-> next-> id> $ id) {$ obj-> next = $ curr-> next; $ curr-> next = $ obj; return true ;} else if ($ curr-> next-> id = $ id) {echo 'current Id :'. $ id. 'repeated. please do not add it again! '; Echo'
'; Return false ;}$ curr = $ curr-> next;} // add a node to the end if ($ curr-> next = null) {$ curr-> next = $ obj;}/*** delete node */public static function del ($ head, $ id) {$ curr = $ head; while ($ curr-> next! = Null) {if ($ curr-> next-> id = $ id) {$ curr-> next = $ curr-> next; return true ;} $ curr = $ curr-> next;}/*** modify node */public static function edit ($ head, $ id, $ new_name) {$ curr = $ head; while ($ curr-> next! = Null) {if ($ curr-> next-> id = $ id) {$ curr-> next-> name = $ new_name ;} $ curr = $ curr-> next ;}}$ head = new linkList (); linkList: add ($ head, 1, 'wangdk '); linkList :: add ($ head, 2, 'sunshuzhen'); linkList: add ($ head, 8, 'hanghahaha'); linkList: add ($ head, 6, 'hangzhoufen'); linkList: add ($ head, 6, 'hangzhoufen'); linkList: add ($ head, 3, 'hangday'); linkList :: del ($ head, 1); linkList: edit ($ head, 2, 'hahahaha '); LinkList: echo_link_list ($ head);?>
Add, query, modify, and delete a linked list:
<? Php/*** PHP basic operations for implementing the linked list */class linkList {/*** name * @ var string */public $ name = ''; /*** No. * @ var int */public $ id = 0;/** reference the next object */public $ next = null; /*** constructors initialize data * @ param int $ id * @ param string $ name */public function _ construct ($ id = 0, $ name = '') {$ this-> name = $ name; $ this-> id = $ id;}/*** Prepaid table */public static function echo_link_list ($ head) {$ curr = $ head; while ($ curr- > Next! = Null) {echo 'name: '. $ curr-> next-> name,' No.: '. $ curr-> next-> id; echo'
'; $ Curr = $ curr-> next ;}/ *** add a new node */public static function add ($ head, $ id, $ name) {$ curr = $ head; $ obj = new linkList ($ id, $ name); while ($ curr-> next! = Null) {// if the current ID <next ID, add it to the center and add the node to the specified sequence position if ($ curr-> next-> id> $ id) {$ obj-> next = $ curr-> next; $ curr-> next = $ obj; return true ;} else if ($ curr-> next-> id = $ id) {echo 'current Id :'. $ id. 'repeated. please do not add it again! '; Echo'
'; Return false ;}$ curr = $ curr-> next;} // add a node to the end if ($ curr-> next = null) {$ curr-> next = $ obj;}/*** delete node */public static function del ($ head, $ id) {$ curr = $ head; while ($ curr-> next! = Null) {if ($ curr-> next-> id = $ id) {$ curr-> next = $ curr-> next; return true ;} $ curr = $ curr-> next;}/*** modify node */public static function edit ($ head, $ id, $ new_name) {$ curr = $ head; while ($ curr-> next! = Null) {if ($ curr-> next-> id = $ id) {$ curr-> next-> name = $ new_name ;} $ curr = $ curr-> next ;}}$ head = new linkList (); linkList: add ($ head, 1, 'wangdk '); linkList :: add ($ head, 2, 'sunshuzhen'); linkList: add ($ head, 8, 'hanghahaha'); linkList: add ($ head, 6, 'hangzhoufen'); linkList: add ($ head, 6, 'hangzhoufen'); linkList: add ($ head, 3, 'hangday'); linkList :: del ($ head, 1); linkList: edit ($ head, 2, 'hahahaha '); LinkList: echo_link_list ($ head);?>