一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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久久无色码中文字幕 | 国产区免费 | 特大巨黑吊在线播放 | 91情侣在线偷精品国产 | 在线观看91精品国产不卡免费 | 狠狠综合久久久久综合小说网 | 成人免费视频在线观看 | 麻豆视屏| 加勒比一区在线 | 337p欧美超大胆日本人术艺术 | 精品视频免费在线观看 | 色天天综合色天天碰 | 欧美激情乱人伦 | 精品久久久久久午夜 | 岛国a视频| 伊人网在线免费视频 | 91青青草视频在线观看 | 91久久综合 | www.亚洲一区二区三区 | 天天综合网在线 | 国产婷婷色综合成人精品 | 国产高清精品久久久久久久 | 天天干天天干天天 | 91中文在线 | 国产精品美女久久久久 | 97色精品视频在线观看免费 | 亚洲二区在线视频 | 黄网在线播放 | 色欧美与xxxxx | 日韩欧美三区 | 无遮挡一级毛片呦女视频 | 国产午夜毛片 | 久久成人免费网站 | 日韩亚洲国产综合久久久 | 伊人加勒比 | 欧美在线观看免费一区视频 | 亚洲卡5卡6卡7卡2021入口 | 男人精品网站一区二区三区 | 成zzzwww日本免费 | 国产婷婷色一区二区三区 |