[PHP] linked list data structure (single-chain table), single-chain php Table Data Structure
Linked List: it is an ordered list, but it is stored discretely in the memory. Using a linked list can solve problems like Joseph, sorting, searching, and generalized tables.
One-way linked list, two-way linked list, and ring linked list
The underlying layer of PHP is C. When a program runs, the memory is divided into five areas (heap, stack, global, constant, and code)
Rule: basic data type, usually in the stack Area
Composite data types, such as objects, are stored in the heap area.
Define a class Hero
Define member attribute ranking $ no
Define member attribute name $ name
Define member attribute nickname $ nickname
Defines the member attribute $ next, which is a reference pointing to the next Hero object.
Define the constructor and pass the following parameters: $ no, $ name, $ nickname
Create a head. This head is only a header and is not put into data.
Get $ head object, new Hero ()
Get the first Hero object $ hero, new Hero (1, "", "timely rain ")
Connect two objects, $ head-> next = $ hero
Get the second Hero object $ hero2, new Hero (2, "Lu Junyi", "Yu Qilin ")
Connect two objects, $ hero-> next = $ hero2
Traversal chain table
Defines a function showHeros (). The parameter is $ head object.
Define a temporary variable $ cur to store $ head objects
While LOOP, condition $ cur-> next is not null
Print it
Move the pointer behind, $ cur = $ cur-> next
PHP version:
<? Php/*** Hero class */class Hero {public $ no; public $ name; public $ nickname; public $ next = null; public function _ construct ($ no = '', $ name ='', $ nickname = '') {$ this-> no = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname;} class LinkListDemo {public static function main () {$ head = new Hero (); $ hero1 = new Hero (1, "", "timely rain"); $ head-> next = $ hero1; $ hero2 = new Hero (2, "Lu Junyi ", "Yu Qilin"); $ hero1-> next = $ hero2; LinkListDemo: show Heros ($ head);}/*** show hero */public static function showHeros ($ head) {$ cur = $ head; while ($ cur-> next! = Null) {echo "name :". $ cur-> next-> name. "<br/>"; $ cur = $ cur-> next ;}} LinkListDemo: main ();
Java version:
Class Hero {public int no; public String name; public String nickname; public Hero next = null; public Hero () {} public Hero (int no, String name, String nickname) {this. no = no; this. name = name; this. nickname = nickname;} public class LinkListDemo {/*** @ param args */public static void main (String [] args) {Hero head = new Hero (); hero hero1 = new Hero (1, "", "timely rain"); head. next = hero1; Hero hero2 = new Hero (2, "Lu Junyi", "Yu Qilin"); hero1.next = hero2; showHeros (head );} /*** display Hero * @ param head */public static void showHeros (Hero head) {Hero cur = head; while (cur. next! = Null) {System. out. println ("name:" + cur. next. name); cur = cur. next ;}}}