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

主站蜘蛛池模板: 亚洲人成无码久久久AAA片 | 在线播放无码字幕亚洲 | 亚洲AV久久久噜噜噜噜 | 熟女人妻-蜜臀AV-首页 | 伊人久久青草青青综合 | 久草草在线视视频 | 综合一区无套内射中文字幕 | 久久久精品日本一区二区三区 | 亚洲黄色在线视频 | 在线视频网站www色 在线视频免费国产成人 | 日日噜噜夜夜爽爽 | 2020美女视频黄频大全视频 | 5g在线视讯年龄确认海外禁止进入 | 香蕉精品国产高清自在自线 | 亚洲高清国产拍精品动图 | 视频一区亚洲中文字幕 | 久热久热精品在线观看 | 日本特黄的免费大片视频 | 三叶草未满十八岁 | 18禁无遮挡羞羞污污污污免费 | 午夜爽喷水无码成人18禁三级 | 久久99re66热这里只有精品 | bt成人种子| 国产成人在线视频播放 | 国精产品一区一区三区有限公司 | 俄罗斯XX性幻女18 | 囯产愉拍亚洲精品一区 | 任你躁国语自产二区在线播放 | 伊人久久大香线蕉电影院 | 久久 这里只精品 免费 | 最新亚洲一区二区三区四区 | 不知火舞vs精子 | 被肉日常np高h | 2019香蕉在线观看直播视频 | 天美传媒色情原创精品 | 亚洲福利天堂网福利在线观看 | 毛片免费在线播放 | 情浓野战台湾三级 | 玩50岁四川熟女大白屁股直播 | 国产传媒在线观看 | 柠檬福利精品视频导航 |