A brief discussion on the data structure of the PHP list (single-linked list), a simple _php tutorial

Source: Internet
Author: User

A brief discussion on the data structure of the PHP linked list (single link list)


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 $hero2,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 */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 ();    $hero 1=new Hero (1, "Song Jiang", "timely Rain");    $head->next= $hero 1;    $hero 2=new Hero (2, "Lu Junyi", "Jade Kylin");    $hero 1->next= $hero 2;    Linklistdemo::showheros ($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 Edition:

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 H Ead=new Hero ();        Hero hero1=new Hero (1, "Song Jiang", "timely Rain");    Head.next=hero1;    Hero hero2=new Hero (2, "Lu Junyi", "Jade Kylin");    Hero1.next=hero2;    Showheros (head);  }  /**   * Show Heroes   * @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;}}}  

This article on the PHP linked list data structure (single-linked list) is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.

http://www.bkjia.com/PHPjc/1135014.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135014.html techarticle a brief talk on the data structure of the PHP chain list (single linked list): A list of single-link lists, but it is distributed in memory, the use of linked lists can be solved similar to Joseph problem, platoon ...

  • 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.