Home >Backend Development >PHP Tutorial >[PHP] Linked list data structure (singly linked list), php table data structure single link_PHP tutorial
Linked list: It is an ordered list, but it is stored dispersedly in memory , using linked lists can solve problems like Joseph's problem, sorting problem, search problem, generalized list
One-way linked list, two-way linked list, circular linked list
The bottom layer of
PHP is C. When a program is running, the memory is divided into five areas (heap area, stack area, global area, constant area, code area )
Provisions: Basic data types, generally placed in the stack area
Composite data types, such as objects, are placed in the heap area
Define a class Hero
Define member attribute ranking $no
Define member attribute name $name
Define member attribute nickname $nickname
Define member attributes $next, which is a reference pointing to the next Hero object
Define the constructor and pass parameters: $no, $name, $nickname
Create a header head. The head is just a header and does not put data
Get the $head object, new Hero()
Get the first Hero object$hero, new Hero(1,"Song Jiang" ,"Timely Rain")
Connect two objects, $head->next=$hero
Get the second Hero object$hero2, new Hero(2,"Lu Junyi" ,"Jade Qilin")
Connect two objects, $hero->next=$hero2
Traverse the linked list
Define a function showHeros(), parameters: $headobject
Define a temporary variable $cur to store the $head object
while loop, condition $cur->next is not null
Print it
The pointer moves backward, $cur=$cur->next
PHP version:
<?<span>php </span><span>/*</span><span>* * 英雄类 </span><span>*/</span> <span>class</span><span> Hero{ </span><span>public</span> <span>$no</span><span>; </span><span>public</span> <span>$name</span><span>; </span><span>public</span> <span>$nickname</span><span>; </span><span>public</span> <span>$next</span>=<span>null</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$no</span>='',<span>$name</span>='',<span>$nickname</span>=''<span>){ </span><span>$this</span>->no=<span>$no</span><span>; </span><span>$this</span>->name=<span>$name</span><span>; </span><span>$this</span>->nickname=<span>$nickname</span><span>; } } </span><span>class</span><span> LinkListDemo{ </span><span>public</span> <span>static</span> <span>function</span><span> main(){ </span><span>$head</span>=<span>new</span><span> Hero(); </span><span>$hero1</span>=<span>new</span> Hero(1,"宋江","及时雨"<span>); </span><span>$head</span>-><span>next</span>=<span>$hero1</span><span>; </span><span>$hero2</span>=<span>new</span> Hero(2,"卢俊义","玉麒麟"<span>); </span><span>$hero1</span>-><span>next</span>=<span>$hero2</span><span>; LinkListDemo</span>::showHeros(<span>$head</span><span>); } </span><span>/*</span><span>* * 展示英雄 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> showHeros(<span>$head</span><span>){ </span><span>$cur</span>=<span>$head</span><span>; </span><span>while</span>(<span>$cur</span>-><span>next</span>!=<span>null</span><span>){ </span><span>echo</span> "姓名:".<span>$cur</span>-><span>next</span>->name."<br/>"<span>; </span><span>$cur</span>=<span>$cur</span>-><span>next</span><span>; } } } LinkListDemo</span>::main();
java version:
<span>class</span><span> Hero{ </span><span>public</span> <span>int</span><span> no; </span><span>public</span><span> String name; </span><span>public</span><span> String nickname; </span><span>public</span> Hero next=<span>null</span><span>; </span><span>public</span><span> Hero(){ } </span><span>public</span> Hero(<span>int</span><span> no,String name,String nickname) { </span><span>this</span>.no=<span>no; </span><span>this</span>.name=<span>name; </span><span>this</span>.nickname=<span>nickname; } } </span><span>public</span> <span>class</span><span> LinkListDemo { </span><span>/**</span><span> * </span><span>@param</span><span> args </span><span>*/</span> <span>public</span> <span>static</span> <span>void</span><span> main(String[] args) { Hero head</span>=<span>new</span><span> Hero(); Hero hero1</span>=<span>new</span> Hero(1, "宋江", "及时雨"<span>); head.next</span>=<span>hero1; Hero hero2</span>=<span>new</span> Hero(2, "卢俊义", "玉麒麟"<span>); hero1.next</span>=<span>hero2; showHeros(head); } </span><span>/**</span><span> * 展示英雄 * </span><span>@param</span><span> head </span><span>*/</span> <span>public</span> <span>static</span> <span>void</span><span> showHeros(Hero head){ Hero cur</span>=<span>head; </span><span>while</span>(cur.next!=<span>null</span><span>){ System.out.println(</span>"姓名:"+<span>cur.next.name); cur</span>=<span>cur.next; } } }</span>