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

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

主站蜘蛛池模板: 国产色婷亚洲99精品AV在 | 人妻美妇疯狂迎合 | 超碰在线 视频 | 老师好爽你下面水好多视频 | 中文字幕精品无码一区二区 | 亚洲精品久久久无码AV片软件 | 老师我好爽再深一点老师好涨 | 中文字幕免费视频精品一 | 破女在线观看视频 | 热综合一本伊人久久精品 | 午夜国产精品影院在线观看 | 中文日产无乱码AV在线观 | 久久热精品18国产 | 亚洲福利区 | 亚洲综合春色另类久久 | 一道精品视频一区二区三区 | 天天操天天干天天爽 | 2021扫黑风暴在线观看免费完整版 | 99久久久无码国产精品免费人妻 | 欧洲电影巜肉欲丛林 | 99视频免费在线观看 | 肉肉描写很细致的黄文 | 久久视频在线视频 | 无码人妻视频又大又粗欧美 | 男女肉大捧进出全过程免费 | 久久综合色一综合色88 | 99久久99久久精品 | 亚洲国产女人aaa毛片在线 | 蜜臀AV久久国产午夜福利软件 | 中字幕久久久人妻熟女天美传媒 | 一个吃奶两个添下面H | japanesematur乱儿| 一个人的视频全免费在线观看www | 99综合之综合久久伊人 | 999人在线精品播放视频 | 秋霞电影网午夜一级鲁丝片 | 国产免费怕怕免费视频观看 | 无限资源在线看影院免费观看 | 在线日本v二区不卡 | 免费的av不用播放器的 | 国产精品亚洲欧美 |