Basic operation examples for simulating linked lists and linked lists in PHP. Basic operation examples
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 '<br>'; $ curr = $ curr-> next ;}} /*** add 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' <br> '; 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 '<br>'; $ curr = $ curr-> next ;}} /*** add 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' <br> '; 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);?>
Articles you may be interested in:
- Analysis of php linked list usage examples
- Introduction and use of PHP double-stranded table
- PHP small tutorial implementation of two-way linked list
- PHP small tutorial implementation linked list
- Example code for php to implement a single-chain table