一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php實現單鏈表的實例代碼

復制代碼 代碼如下:
<?php

//鏈表節點
class node {
    public $id; //節點id
    public $name; //節點名稱
    public $next; //下一節點
   

    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
        $this->next = null;
    }
}

//單鏈表
class singelLinkList {
    private $header; //鏈表頭節點
   

    //構造方法
    public function __construct($id = null, $name = null) {
        $this->header = new node ( $id, $name, null );
    }

    //獲取鏈表長度
    public function getLinkLength() {
        $i = 0;
        $current = $this->header;
        while ( $current->next != null ) {
            $i ++;
            $current = $current->next;
        }
        return $i;
    }

    //添加節點數據
    public function addLink($node) {
        $current = $this->header;
        while ( $current->next != null ) {
            if ($current->next->id > $node->id) {
                break;
            }
            $current = $current->next;
        }
        $node->next = $current->next;
        $current->next = $node;
    }

    //刪除鏈表節點
    public function delLink($id) {
        $current = $this->header;
        $flag = false;
        while ( $current->next != null ) {
            if ($current->next->id == $id) {
                $flag = true;
                break;
            }
            $current = $current->next;
        }
        if ($flag) {
            $current->next = $current->next->next;
        } else {
            echo "未找到id=" . $id . "的節點!<br>";
        }
    }

    //獲取鏈表
    public function getLinkList() {
        $current = $this->header;
        if ($current->next == null) {
            echo ("鏈表為空!");
            return;
        }
        while ( $current->next != null ) {
            echo 'id:' . $current->next->id . '   name:' . $current->next->name . "<br>";
            if ($current->next->next == null) {
                break;
            }
            $current = $current->next;
        }
    }

    //獲取節點名字
    public function getLinkNameById($id) {
        $current = $this->header;
        if ($current->next == null) {
            echo "鏈表為空!";
            return;
        }
        while ( $current->next != null ) {
            if ($current->id == $id) {
                break;
            }
            $current = $current->next;
        }
        return $current->name;
    }

    //更新節點名稱
    public function updateLink($id, $name) {
        $current = $this->header;
        if ($current->next == null) {
            echo "鏈表為空!";
            return;
        }
        while ( $current->next != null ) {
            if ($current->id == $id) {
                break;
            }
            $current = $current->next;
        }
        return $current->name = $name;
    }
}

$lists = new singelLinkList ();
$lists->addLink ( new node ( 5, 'eeeeee' ) );
$lists->addLink ( new node ( 1, 'aaaaaa' ) );
$lists->addLink ( new node ( 6, 'ffffff' ) );
$lists->addLink ( new node ( 4, 'dddddd' ) );
$lists->addLink ( new node ( 3, 'cccccc' ) );
$lists->addLink ( new node ( 2, 'bbbbbb' ) );
$lists->getLinkList ();
echo "<br>-----------刪除節點--------------<br>";
$lists->delLink ( 5 );
$lists->getLinkList ();

echo "<br>-----------更新節點名稱--------------<br>";
$lists->updateLink ( 3, "222222" );
$lists->getLinkList ();

echo "<br>-----------獲取節點名稱--------------<br>";
echo $lists->getLinkNameById ( 5 );

echo "<br>-----------獲取鏈表長度--------------<br>";
echo $lists->getLinkLength ();
?>

php技術php實現單鏈表的實例代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 一级特黄国产高清毛片97看片 | 精品久久久久久久久久 | 在线一区免费视频播放 | 香蕉成人啪国产精品视频综合网 | 午夜在线播放视频 | www色.com | 一区二区高清视频 | 中文字幕日韩理论在线 | 国产精品hd | 四虎影视永久免费视频观看 | 91在线视频导航 | 亚洲高清视频网站 | 唐人社电亚洲一区二区三区 | 一区二区中文字幕 | 国语自产免费精品视频一区二区 | 亚洲欧美视频在线播放 | 91免费国产 | 日本精品一区二区三本中文 | www色在线观看 | 亚洲午夜在线视频 | 亚洲fuli在线观看 | 五月婷婷六月综合 | 国产91丝袜香蕉在线播放 | 国产永久在线 | 国产一区视频在线免费观看 | 全部免费69堂在线视频 | 国产成人福利免费视频 | 欧美大成色www永久网站婷 | 欧美日韩国产超高清免费看片 | 伊人三区 | 91成人免费视频 | 中文字幕久久网 | 韩国亚洲伊人久久综合影院 | 欧美福利视频 | 中文字幕久久久久久久系列 | 四虎在线永久免费视频网站 | 七七国产福利在线二区 | 最近手机中文字幕1页 | 成人a视频| 奇米影视99 | 国产成人精品免费午夜 |