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