header("Content-type:text/html;charset=utf-8");
//鏈表節點
class node {
public static $count = -1; //節點id
public $name; //節點名稱
public $next; //下一節點
public $id;
public function __construct($name) {
$this->id = self::$count;
$this->name = $name;
$this->next = null;
self::$count += 1;
}
}
//單鏈表
class singelLinkList {
private $header;
private $current;
public $count;
//構造方法
public function __construct($data = null) {
$this->header = new node ($data);
$this->current = $this->header;
$this->count = 0;
}
//添加節點資料
public function addLink($node) {
if($this->current->next != null)
$this->current = $this->current->next;
$this->count++;
$node->next = $this->current->next;
$this->current->next = $node;
}