PHP linked list

Source: Internet
Author: User

I have read the data structure for a long time, but I have never used it. I have read the data structure of PHP on the Internet and shared it with you.

Brief cut-off

Class Hero {public $ no; // rank public $ name; // name public $ next = null; // $ next is a reference, public function _ construct ($ no = '', $ name ='') {$ this-> no = $ no; $ this-> name = $ name;} static public function showList ($ head) {$ cur = $ head; while ($ cur-> next! = Null) {echo "ranking :". $ cur-> next-> no. ", name :". $ cur-> next-> name. "<br>"; $ cur = $ cur-> next ;}}
// Insert the static public function addHero ($ head, $ hero) {$ cur = $ head; while ($ cur-> next! = Null) {$ cur = $ cur-> next;} $ cur-> next = $ hero;} // insert static public function addHeroSorted ($ head, $ hero) {$ cur = $ head; $ addNo = $ hero-> no; while ($ cur-> next-> no <= $ addNo) {$ cur = $ cur-> next;}/* $ tep = new Hero (); $ tep = $ cur-> next; $ cur-> next = $ hero; $ hero-> next = $ tep; */$ hero-> next = $ cur-> next; $ cur-> next = $ hero ;} static public function deleteHero ($ head, $ no) {$ cur = $ head; while ($ cur-> n Ext-> no! = $ No & $ cur-> next! = Null) {$ cur = $ cur-> next;} if ($ cur-> next-> no! = Null) {$ cur-> next = $ cur-> next; echo "deleted successfully <br> ";} else {echo "not found <br>" ;}} static public function updateHero ($ head, $ hero) {$ cur = $ head; while ($ cur-> next-> no! = $ Hero-> no & $ cur-> next! = Null) {$ cur = $ cur-> next;} if ($ cur-> next-> no! = Null) {$ hero-> next = $ cur-> next; $ cur-> next = $ hero; echo "changed successfully <br> ";} else {echo "not found <br>" ;}}// create head header $ head = new Hero (); // the first $ hero = new Hero (1, '20140901'); // connect $ head-> next = $ hero; // The second $ hero2 = new Hero (3, '20160901'); // connect Hero :: addHero ($ head, $ hero2); $ hero3 = new Hero (2, '000000'); Hero: addHeroSorted ($ head, $ hero3); // display Hero :: showlist ($ head); // Delete Hero: deleteHero ($ head, 4); // display Hero: showlist ($ head ); // change $ hero4 = new Hero (2, 'xxx'); Hero: updateHero ($ head, $ hero4); // display Hero: showlist ($ head );

I am the dividing line of tiantiao

For ordered inserts, You need to traverse the linked list. The knowledge of the linked list is not introduced. Here we will mainly share the code.

 

 

Reprinted please indicate the source: http://www.cnblogs.com/yydcdut

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.