Basic operation examples for simulating linked lists and linked lists in PHP. Basic operation examples

Source: Internet
Author: User

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

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.