这段代码我认为有些缺陷,是什么呢?就是类型的约束不够严格!php本身是弱类型的语言!
所以在这一块有些迷茫了!忘指点!本人系新手!
下面是代码
节点类
class node{ private $data; // 节点数据 private $next; // 下一节点 public function setdata($value){ $ this -> data = $value; } public function setnext($value){ $ this -> next = $value; } public function getdata(){ return $ this -> data; } public function getnext(){ return $ this -> next; } public function __construct($data,$next){ $ this -> setdata($data); $ this -> setnext($next); } }
功能类
class linklist{ private $header; // 头节点 private $size; // 长度 public function getsize(){ $i = 0 ; $node = $ this -> header; while ($node -> getnext() != null ) { $i ++ ; $node = $node -> getnext(); } return $i; } public function setheader($value){ $ this -> header = $value; } public function getheader(){ return $ this -> header; } public function __construct(){ header( content-type:text/html; charset=utf-8 ); $ this -> setheader( new node( null , null )); } /* * *@author mzxy *@param $data--要添加节点的数据 * */ public function add($data) { $node = $ this -> header; while ($node -> getnext() != null ) { $node = $node -> getnext(); } $node -> setnext( new node($data, null )); } /* * *@author mzxy *@param $data--要移除节点的数据 * */ public function removeat($data) { $node = $ this -> header; while ($node -> getdata() != $data) { $node = $node -> getnext(); } $node -> setnext($node -> getnext()); $node -> setdata($node -> getnext() -> getdata()); } /* * *@author mzxy *@param 遍历 * */ public function get () { $node = $ this -> header; if ($node -> getnext() == null ){ print( 数据集为空! ); return ; } while ($node -> getnext() != null ) { print($node -> getnext() -> getdata()); if ($node -> getnext() -> getnext() == null ){ break ;} $node = $node -> getnext(); } } /* * *@author mzxy *@param $data--要访问的节点的数据 * @param 此方法只是演示不具有实际意义 * */ public function getat($data) { $node = $ this -> header -> getnext(); if ($node -> getnext() == null ){ print( 数据集为空! ); return ; } while ($node -> getdata() != $data) { if ($node -> getnext() == null ){ break ;} $node = $node -> getnext(); } return $node -> getdata(); } /* * *@author mzxy *@param $value--需要更新的节点的原数据 --$initial---更新后的数据 * */ public function update($initial,$value) { $node = $ this -> header -> getnext(); if ($node -> getnext() == null ){ print( 数据集为空! ); return ; } while ($node -> getdata() != $data) { if ($node -> getnext() == null ){ break ;} $node = $node -> getnext(); } $node -> setdata($initial); } }
