PHP linked list data structure (single-chain table); single-chain _ PHP Tutorial

Source: Internet
Author: User
Let's talk about the data structure of the PHP linked list (single-chain table) and single-chain. Talking about the data structure of the PHP linked list (single-chain table), talking about the single-chain list: it is an ordered list, but it is stored discretely in the memory. Using the linked list can solve problems similar to Joseph, comment on the PHP linked list data structure (single-chain table) and single-chain

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 ."
"; $ 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 ;}}}

The above discussion about the data structure of the PHP linked list (single-chain table) is all the content shared by the editor. I hope to give you a reference and support for the help house.

A single linked list (single-chain table) is an ordered list, but it is stored discretely in the memory. Using the linked list can solve the problem similar to Joseph, sorting...

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.