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

主站蜘蛛池模板: 国产精品无码久久久久不卡 | 大胸女晃奶动态图 | 亚洲a视频在线 | 97国产蝌蚪视频在线观看 | 精品熟女少妇AV免费观看 | 国产成人精品永久免费视频 | 色男人的天堂久久综合 | 中国明星16xxxxhd | 国产片MV在线观看 | 男人插曲视频大全免费网站 | 0951影音在线 | 免费人成视频X8X8国产更快乐 | 99久久99久久久精品久久 | 真人裸交有声性动态图 | 少男同志freedeos | 国产不卡视频在线观看 | 亚洲精品午夜VA久久成人 | 欧美三级aaa | 偷拍自偷拍亚洲精品 | 久久亚洲国产中文字幕 | 皮皮色狼网 | 午夜天堂AV久久久噜噜噜 | 男助理憋尿PLAY灌尿BL出去 | 美女扒开尿口直播 | 国产 日韩 欧美 高清 亚洲 | 日本高清二区 | 精品熟女少妇AV免费观看 | 国产欧美亚洲综合第一页 | 极品色αv影院 | 在线a亚洲视频 | georgielyall装修工 | 九九精品视频一区二区三区 | 十8禁用B站在线看漫画 | 永久adc视频年龄确认 | 中文字幕精品AV内射夜夜夜 | 新影音先锋男人色资源网 | 九九热在线免费观看 | a亚洲在线观看不卡高清 | 92国产精品午夜免费福利视频 | 和美女啪啪啪动态图 | 久久本道久久综合伊人 |