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

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

主站蜘蛛池模板: 亚洲视频在线免费 | 波多结衣一区二区三区 | 国产最猛性XXXX69交 | 国产精品免费一区二区区 | 久久久久国产一级毛片高清片 | 精品无码久久久久久动漫 | 快播av网址 | 天天看高清影视在线18 | 猫咪av永久最新域名 | 在线看片亚洲 | 欧美精品成人一区二区在线观看 | YIN荡的老师系列第6部分视频 | 亚洲VA欧美VA天堂V国产综合 | 亚洲AV人无码综合在线观看蜜桃 | 草莓视频在线观看完整高清免费 | 国产成人综合高清在线观看 | 伦理片在线线手机版韩国免费6 | 免费观看桶机十分钟 | 亚洲中文日韩日本在线视频 | 十八禁啪啦啪漫画 | 国产亚洲精品精品精品 | 激情床戏揉胸吃胸视频 | 国产亚洲精品成人AV久久 | 欧美日韩视频高清一区 | 偷拍精品视频一区二区三区 | 扒开粉嫩的小缝末成年小美女 | 快播黄色电影 | 精品欧美一区二区三区四区 | 国产女高清在线看免费观看 | 国产午夜视频在线 | 美女张开腿让男人桶爽无弹窗 | 久久国产精品人妻中文 | 美女被触手注入精子强制受孕漫画 | 高清 仑乱 一级 a | 毛片免费在线播放 | 久久精品国产亚洲AV忘忧草蜜臀 | 被公疯狂玩弄的漂亮人妻 | 69丰满少妇AV无码区 | 51vv视频社区| 国产精品97久久久久久AV色戒 | 91传媒蜜桃香蕉在线观看 |