php實現簡單的單鏈表

來源:互聯網
上載者:User

標籤: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實現簡單的單鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.