background image

PHP 代码:php 实现单链表的实例代码

以下为大家分享 php 实现单链表的实例代码,
代码如下:
 
<?php

//

 

链表节点

class

 node { 

public

 

$id

//节点 id 

public

 

$name

//

 

节点名称

public

 

$next

//

 

下一节点

public

 

function

 __construct(

$id

$name

) { 

$this

->id = 

$id

$this

->name = 

$name

$this

->next = null; 


}

//

 

单链表

class

 singelLinkList { 

private

 

$header

//

 

链表头节点

//

 

构造方法

public

 

function

 __construct(

$id

 = null, 

$name

 = null) { 

$this

->header = 

new

 node ( 

$id

$name

, null ); 


 

//

 

获取链表长度

public

 

function

 getLinkLength() { 

$i

 = 0; 

$current

 = 

$this

->header; 

while

 ( 

$current

->next != null ) { 

$i

 ++; 

$current

 = 

$current

->next; 

return

 

$i


 

//

 

添加节点数据