PHP中類比鏈表和鏈表的基本操作樣本,基本操作樣本_PHP教程

來源:互聯網
上載者:User

PHP中類比鏈表和鏈表的基本操作樣本,基本操作樣本


類比鏈表:

<?php /** * PHP實現鏈表的基本操作 */class linkList {  /**   * 姓名   * @var string   */  public $name = '';     /**   * 編號   * @var int   */  public $id = 0;     /*   * 引用下一個對象   */  public $next = null;     /**   * 建構函式初始化資料   * @param int $id   * @param string $name   */  public function __construct($id = 0, $name = '') {    $this->name = $name;    $this->id  = $id;  }     /**   * 遍曆鏈表   */  public static function echo_link_list($head) {    $curr = $head;    while ($curr->next != null) {      echo '姓名:'.$curr->next->name, ' 編號:'.$curr->next->id;      echo '
'; $curr = $curr->next; } } /** * 添加新節點 */ public static function add($head, $id, $name) { $curr = $head; $obj = new linkList($id, $name); while ($curr->next != null) { // 如果當前ID < 下一個ID,則添加到中間,添加節點到指定順序位置 if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id == $id) { echo '當前Id:'.$id.'重複了,請不要繼續添加了!'; echo '
'; return false; } $curr = $curr->next; } // 添加節點到尾部 if ($curr->next == null) { $curr->next = $obj; } } /** * 刪除節點 */ public static function del($head, $id) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next = $curr->next->next; return true; } $curr = $curr->next; } } /** * 修改節點 */ public static function edit($head, $id, $new_name) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next->name = $new_name; } $curr = $curr->next; } }} $head = new linkList();linkList::add($head, 1, 'wangdk');linkList::add($head, 2, 'sunshuzhen');linkList::add($head, 8, 'wanghaha');linkList::add($head, 6, 'wangchufen');linkList::add($head, 6, 'wangchufen');linkList::add($head, 3, 'wangdaye'); linkList::del($head, 1);linkList::edit($head, 2, 'hahaha');linkList::echo_link_list($head); ?>

鏈表的增刪查改:

<?php /** * PHP實現鏈表的基本操作 */class linkList {  /**   * 姓名   * @var string   */  public $name = '';     /**   * 編號   * @var int   */  public $id = 0;     /*   * 引用下一個對象   */  public $next = null;     /**   * 建構函式初始化資料   * @param int $id   * @param string $name   */  public function __construct($id = 0, $name = '') {    $this->name = $name;    $this->id  = $id;  }     /**   * 遍曆鏈表   */  public static function echo_link_list($head) {    $curr = $head;    while ($curr->next != null) {      echo '姓名:'.$curr->next->name, ' 編號:'.$curr->next->id;      echo '
'; $curr = $curr->next; } } /** * 添加新節點 */ public static function add($head, $id, $name) { $curr = $head; $obj = new linkList($id, $name); while ($curr->next != null) { // 如果當前ID < 下一個ID,則添加到中間,添加節點到指定順序位置 if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id == $id) { echo '當前Id:'.$id.'重複了,請不要繼續添加了!'; echo '
'; return false; } $curr = $curr->next; } // 添加節點到尾部 if ($curr->next == null) { $curr->next = $obj; } } /** * 刪除節點 */ public static function del($head, $id) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next = $curr->next->next; return true; } $curr = $curr->next; } } /** * 修改節點 */ public static function edit($head, $id, $new_name) { $curr = $head; while($curr->next != null) { if ($curr->next->id == $id) { $curr->next->name = $new_name; } $curr = $curr->next; } }} $head = new linkList();linkList::add($head, 1, 'wangdk');linkList::add($head, 2, 'sunshuzhen');linkList::add($head, 8, 'wanghaha');linkList::add($head, 6, 'wangchufen');linkList::add($head, 6, 'wangchufen');linkList::add($head, 3, 'wangdaye'); linkList::del($head, 1);linkList::edit($head, 2, 'hahaha');linkList::echo_link_list($head); ?>

您可能感興趣的文章:

  • php鏈表用法執行個體分析
  • PHP 雙鏈表(SplDoublyLinkedList)簡介和使用執行個體
  • PHP小教程之實現雙向鏈表
  • PHP小教程之實現鏈表
  • php實現單鏈表的執行個體代碼

http://www.bkjia.com/PHPjc/1104328.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1104328.htmlTechArticlePHP中類比鏈表和鏈表的基本操作樣本,基本操作樣本 類比鏈表: php /** * PHP實現鏈表的基本操作 */class linkList { /** * 姓名 * @var string */ publ...

  • 聯繫我們

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