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

php實現(xiàn)單鏈表的實例代碼

復(fù)制代碼 代碼如下:
<?php

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

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

//單鏈表
class singelLinkList {
    private $header; //鏈表頭節(jié)點
   

    //構(gòu)造方法
    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;
    }

    //添加節(jié)點數(shù)據(jù)
    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;
    }

    //刪除鏈表節(jié)點
    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 . "的節(jié)點!<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;
        }
    }

    //獲取節(jié)點名字
    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;
    }

    //更新節(jié)點名稱
    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>-----------刪除節(jié)點--------------<br>";
$lists->delLink ( 5 );
$lists->getLinkList ();

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

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

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

php技術(shù)php實現(xiàn)單鏈表的實例代碼,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 成人无码精品1区2区3区免费看 | 中文字幕亚洲欧美在线视频 | 日本中文字幕伊人成中文字幕 | 国产3级在线观看 | 开心成人社区 | 韩国电影久久 | 精品国产中文字幕在线视频 | 天堂无码人妻精品AV一区 | 免费毛片在线播放 | 青娱乐极品视觉盛宴国产视频 | 色偷偷男人天堂 | 国产在线高清视频无码 | 香港日本三级亚洲三级 | 久久re这里精品在线视频7 | 欧美乱妇15p图 | 成人影院午夜久久影院 | 正能量不良WWW免费窗口 | 国产亚洲一区二区三区啪 | 韩国羞羞秘密教学子开车漫书 | 亚洲高清有码中文字 | 国产色精品久久人妻无码看片软件 | 国产AV果冻传奇麻豆 | 最近中文字幕MV高清在线 | 亚洲国产精品无码中文在线 | 5G年龄确认我已满18免费 | 日产日韩亚洲欧美综合搜索 | 99国产精品久久久久久久日本竹 | 国产盗摄一区二区三区 | 成人永久免费视频 | 入禽太深免费观看 | 国产人妻人伦精品9 | 欧美另类一区 | 成人天堂资源WWW在线 | 午夜精品久久久久久影视riav | 99热都是精品 | 国产中文字幕免费观看 | 九九久久精品 | 一本道无码字幕在线看 | 边做边爱免费视频播放 | 制服国产欧美亚洲日韩 | 国产精品毛片AV久久97 |