码迷,mamicode.com
首页 > Web开发 > 详细

php实现简单的单链表

时间:2014-11-13 12:44:17      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:blog   io   数据   div   on   log   cti   ad   as   

<?php 
//节点类,数据和指向下一节点指针
class Node{
	public $data;
	public $next;
	
	public function __construct($data,$next=null){
		$this->data = $data;
		$this->next = $next;
	}
}

//链表类
class LinkList{
	public $header;
	
    public function __construct(Node $node){//头节点不为空
    	$this->header = $node;
    }
    
    public function add(Node $node){
    	$current = $this->header;
    	while ($current->next!==null){
    		$current = $current->next;
    	}
    	$current->next = $node;
    }
    
    public function show(){
    	$current = $this->header;
    	while ($current->next !== null){
    		echo $current->data;
    		echo "<br/>";
    		$current = $current->next;
    	}
    	echo $current->data;
    }
    
    
}
//头结点为空的链表
class LinkListOne{
	public $header;
	
	public function __construct(){
		$this->header = new Node(null);
	}
	
	public function add(Node $node){
		$current = $this->header;
		while($current->next!==null){
			$current = $current->next;
		}
		$current->next = $node;
	}
	
	public function show(){
		$current = $this->header;
		while($current->next!==null){
			echo $current->next->data;
			echo "<br/>";
			$current = $current->next;
		}
	}
}
class Client{
	public static function main(){
		$node1 = new Node(1);
		$node2 = new Node(2);
		$node3 = new Node(3);
		$node4 = new Node(4);
		$linklist = new LinkListOne();
		$linklist->add($node1);
		$linklist->add($node3);
		$linklist->add($node4);

		$linklist->show();
	}
}
Client::main();

?>

  

php实现简单的单链表

标签:blog   io   数据   div   on   log   cti   ad   as   

原文地址:http://www.cnblogs.com/taijun/p/4094486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!