天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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實現單鏈表的實例代碼,轉載需保留來源!

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

主站蜘蛛池模板: 成 人 免费 黄 色 网站无毒下载 | 亚洲中文字幕国产综合 | 国产视频这里只有精品 | 久久九九精品国产自在现线拍 | 国产精品久久久久婷婷五月色 | 我要搞av| 早乙女由依在线观看 | 97超碰免费人妻中文 | 欧美又粗又长又大AAAA片 | 神马电影院午 夜理论 | 私密按摩师在线观看 百度网盘 | 亲嘴扒胸摸屁股视频免费网站 | 99亚洲精品自拍AV成人软件 | 97视频在线观看免费视频 | 性欧美video 性欧美sexovideotv | 99国产福利 | 久久成人无码国产免费播放 | 99久久无码热高清精品 | 国产精品亚洲精品影院 | 麻豆三级电影 | 老人FREE VIODES老少配 | 99热在线精品视频 | 有码 亚洲 制服 国产 在线 | 国产高清亚洲日韩字幕一区 | 亚洲AV香蕉一区区二区三区蜜桃 | 国产成人教育视频在线观看 | 国产亚洲欧美日韩综合综合二区 | 狠狠啪 日日啪 | 亚洲精品在线看 | 无码国产色欲XXXX视频 | 啊灬啊别停灬用力啊在线观看视频 | 免费观看成人毛片 | 新图解av吧 | 床上色APP下载免费版 | 亚洲A片不卡无码久久尤物 亚洲a免费 | 伊人久久伊人 | AV天堂午夜精品蜜臀AV | 国产久久re6免费热在线 | 免费观看a视频 | 色欲档案之麻雀台上淫 | 国产成人亚洲精品午夜国产馆 |