A brief discussion on _php example of PHP linked list data structure (single chain list)

Source: Internet
Author: User

Linked list: is an ordered listing, but it is distributed in memory, using a linked list can solve problems like Joseph, sorting problems, search questions, generalized table

One-way linked list, two-way linked list, ring linked list

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

Rules: Basic data types, generally placed in the stack area

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


define a class hero

Define the ranking of member attributes $no

Define member property name $name

Defines a member property nickname $nickname

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

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


Create a header head, which is just a header and does not put data

Gets 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 Kylin")

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


traversing a linked list

Define a function Showheros (), Parameter: $head object

Define a temporary variable $cur to store $head objects

While loop, condition $cur->next NOT NULL

Print it, please.

The pointer moves back, $cur = $cur->next

PHP Version:

<?php
/**
* Hero Category * * 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. " <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 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 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 article on the PHP linked list data structure (single linked list) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.