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

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)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 91免费视频版 | 亚洲狠狠婷婷综合久久久久 | 亚色在线观看 | 久久综合网久久综合 | 青青自拍视频一区二区三区 | 91久久国产口精品久久久久 | 青草精品 | 精品久久久久久国产91 | 91福利视频网 | 国产精品无 | 激情五月婷婷综合网 | 国产精品国色综合久久 | a级精品九九九大片免费看 a加勒比一本东京 | 亚洲精品人人 | 亚亚洲乱码一二三四区 | 日本一道本中文字幕 | 高清国产美女一级a毛片录 高清国产美女一级毛片 | 午夜激情一区 | 六月丁香婷婷激情 | 亚洲综合激情小说 | 无套日出白浆在线播放 | 国产精品大白天新婚身材 | 亚洲影视久久 | 精品日韩一区二区三区视频 | 成人午夜视频在线观 | 国产xx肥老妇视频奂费 | 亚洲午夜久久久久国产 | 欧美成人福利视频 | 色综合色综合色综合色综合 | 日本欧美一区二区三区视频 | 天天躁天天弄天天爱 | 97精品国产91久久久久久 | 伊人第四色| 看全色黄大色大片免费 | 欧美色资源 | 91国偷自产一区二区三区 | 国内精品欧美久久精品 | 国产成人精品.一二区 | 亚洲综合色网站 | 夜夜揉揉日日人人视频 | 91精品国产91久久 |