In PHP, the basic operation examples for simulating linked lists and linked lists are as follows. In PHP, the basic operation example for simulating the linked list and linked list is simulated. The basic operation example is to simulate the linked list: php *** PHP implements the basic operation of the linked list * classlinkList {*** name * @ varstring * publ PHP to simulate the basic operation examples of the linked list and the linked list.
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);?>
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
Using simulated linked list: php/*** PHP performs basic operations on the linked list */class linkList {/*** name * @ var string */publ...