天天躁日日躁狠狠躁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)系我們修改或刪除,多謝。

主站蜘蛛池模板: 色婷婷欧美在线播放内射 | 亚洲欧美在无码片一区二区 | 国产亚洲日韩另类在线观看 | 国产精品高清在线观看地址 | 四房播播开心五月 | 特黄AAAAAAA片免费视频 | 一边吃奶一边啪啪真舒服 | 精品视频网站 | 新图解av吧 | 久久精品中文字幕有码日本 | 国产极品美女视频福利 | 三级网址在线观看 | 人妻少妇偷人精品无码洋洋AV | 久久er国产精品免费观看2 | 超碰在线vip | 狠狠色狠狠色88综合日日91 | 让男人玩尿道的女人 | 7723手机游戏破解版下载 | 国产亚洲精品久久无亚洲 | 久久精品热线免费 | 国产欧美精品一区二区三区-老狼 | 小sao货ji巴cao死你视频 | 亚洲精品沙发午睡系列 | 337p啪啪人体大胆 | 精品国产品在线18年 | 午夜爽喷水无码成人18禁三级 | 亚洲AV无码专区国产精品99 | 欧美日韩在线亚洲一 | 色多多污网站在线观看 | 国产成人精品综合在线观看 | 97精品伊人久久大香线蕉app | 国产色青青视频在线观看 | 十分钟免费视频大全在线 | 成人五级毛片免费播放 | 日本VA在线视频播放 | 99日影院在线播放 | 亚洲成在人线视频 | 亚洲免费每日在线观看 | 国产婷婷一区二区在线观看 | 视频一区精品自拍亚洲 | 无码AV毛片色欲欧洲美洲 |