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;
}
}
$head = new Hero(); // 僅作頭節點,不存資料 $hero1 = new Hero(1, "宋江", "及時雨"); $head->next = $hero1; $hero2 = new Hero(2, "盧俊義", "玉麒麟"); $hero1->next = $hero2; // 可繼續串接...
function showHeros($head) {
$cur = $head;
while ($cur->next !== null) {
echo "姓名:{$cur->next->name}
";
$cur = $cur->next;
}
}
showHeros($head);
no=$no; $this->name=$name; $this->nickname=$nickname;
}
}
class LinkListDemo {
public static function main() {
$head = new Hero();
$hero1 = new Hero(1, "宋江", "及時雨");
$head->next = $hero1;
$hero2 = new Hero(2, "盧俊義", "玉麒麟");
$hero1->next = $hero2;
self::showHeros($head);
}
public static function showHeros($head) {
$cur = $head;
while ($cur->next !== null) {
echo "姓名:{$cur->next->name}
";
$cur = $cur->next;
}
}
}
LinkListDemo::main();
?>
class Hero {
public int no; public String name, nickname; public Hero next = null;
public Hero(int no, String name, String nickname) {
this.no=no; this.name=name; this.nickname=nickname;
}
}
public class LinkListDemo {
public static void main(String[] args) {
Hero head = new Hero();
head.next = new Hero(1, "宋江", "及時雨");
head.next.next = new Hero(2, "盧俊義", "玉麒麟");
showHeros(head);
}
static void showHeros(Hero head) {
for (Hero cur = head; cur.next != null; cur = cur.next) {
System.out.println("姓名:" + cur.next.name);
}
}
}
next 指向next 指向被刪節點的 next,PHP GC 自動回收掌握鏈表指標操作,是理解樹、圖等進階資料結構的基石。