[PHP] Linked list data structure (single linked list), PHP table data structure single chain _php tutorial

Source: Internet
Author: User

[PHP] Linked list data structure (single linked list), PHP table data structure single chain


Linked lists: is an ordered list, but it is distributed in memory, using linked lists can solve similar Joseph problems, sorting problems, search problems, generalized tables

Unidirectional linked list, doubly linked list, ring linked list

the bottom of PHP is C, when a program runs, memory is divided into five zones (heap, stack, global, constant, code area)

Rule: Basic data type, generally in the stack area

Composite data types, such as objects, are placed in the heap area

Defines a class of Hero

Define member property rank $no

Define member property name $name

Define member property nickname $nickname

Defines a member property $next, which is a reference, pointing to the next Hero Object

Define constructors, pass parameters:$no,$name,$nickname

Create a header head , the head is just a header, do not put the data

Get $head object,new Hero ()

Gets the first Hero object $hero,new Hero (1,"Song Jiang", "Timely Rain")

Connect two objects,$head->next= $hero

Get the second Hero object $hero 2,new Hero (2,"Lu Junyi", "Jade Kylin")

Connect two objects,$hero->next= $hero 2

Traversing a linked list

Define a function Showheros (), parameter:$head Object

Defines a temporary variable $cur to store $head Objects

while loop, condition $cur->next not null

Print a bit

Pointer moves back,$cur = $cur->next

PHP Version:

 Php/** * Hero class*/classhero{ Public $no;  Public $name;  Public $nickname;  Public $next=NULL;  Public function__construct ($no='',$name='',$nickname=''){        $this->no=$no; $this->name=$name; $this->nickname=$nickname; }}classlinklistdemo{ Public Static functionMain () {$head=NewHero (); $hero 1=NewHero (1, "Song Jiang", "Timely Rain"); $head-Next=$hero 1; $hero 2=NewHero (2, "Lu Junyi", "Jade Kylin"); $hero 1-Next=$hero 2; Linklistdemo:: Showheros ($head); }    /** * Show Heroes*/     Public Static functionShowheros ($head){        $cur=$head;  while($cur-Next!=NULL){            Echo"Name:".$cur-Next->name. "
"; $cur=$cur-Next; }}}linklistdemo:: Main ();

Java Edition:

classhero{ Public intNo;  PublicString name;  PublicString Nickname;  PublicHero next=NULL;  PublicHero () {} PublicHero (intno,string name,string Nickname) {             This. no=No;  This. name=name;  This. nickname=nickname; }    } Public classLinklistdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {Hero head=NewHero (); Hero Hero1=NewHero (1, "Song Jiang", "Timely Rain"); Head.next=Hero1; Hero Hero2=NewHero (2, "Lu Junyi", "Jade Kylin"); Hero1.next=Hero2;    Showheros (head); }    /*** Show Heroes *@paramHead*/     Public Static voidShowheros (Hero head) {Hero cur=Head;  while(cur.next!=NULL) {System.out.println ("Name:" +cur.next.name); Cur=Cur.next; }    }}

http://www.bkjia.com/PHPjc/1127372.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127372.html techarticle [PHP] linked list data structure (single linked list), PHP table data structure single-link list: is an ordered listing, but it is distributed in memory, the use of linked lists can be solved like Joseph ...

  • Related Article

    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.