php鏈表

來源:互聯網
上載者:User
之前在部落格園搜尋發現沒有用php實現的鏈表,在百度也幾乎沒有!所以在這把代碼貼上來!

這段代碼我認為有些缺陷,是什麼呢?就是類型的約束不夠嚴格!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); } }

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.