php如何實(shí)現(xiàn)單鏈表

本文將為大家詳細(xì)介紹“php如何實(shí)現(xiàn)單鏈表”,內(nèi)容步驟清晰詳細(xì),細(xì)節(jié)處理妥當(dāng),而小編每天都會更新不同的知識點(diǎn),希望這篇“php如何實(shí)現(xiàn)單鏈表”能夠給你意想不到的收獲,請大家跟著小編的思路慢慢深入,具體內(nèi)容如下,一起去收獲新知識吧。

創(chuàng)新互聯(lián)建站專注于肥城企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都商城網(wǎng)站開發(fā)。肥城網(wǎng)站建設(shè)公司,為肥城等地區(qū)提供建站服務(wù)。全流程按需制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

php實(shí)現(xiàn)單鏈表的方法:首先寫出鏈表節(jié)點(diǎn)的類;然后在鏈表中還定義兩個方法,分別是插入和刪除;接著獲取鏈表長度并添加節(jié)點(diǎn)數(shù)據(jù);最后獲取節(jié)點(diǎn)名字并進(jìn)行刪除或更新操作即可。

用PHP實(shí)現(xiàn)的單鏈表

單鏈表顧名思義就是一個鏈?zhǔn)綌?shù)據(jù)結(jié)構(gòu),它有一個表頭,并且除了最后一個節(jié)點(diǎn)外,所有節(jié)點(diǎn)都有其后繼節(jié)點(diǎn)。如下圖。

首先,我們寫出鏈表節(jié)點(diǎn)的類。單鏈表中的每一個節(jié)點(diǎn),都保存其數(shù)據(jù)域和后驅(qū)指針

//鏈表節(jié)點(diǎn) 
class node { 
    public $id; //節(jié)點(diǎn)id 
    public $name; //節(jié)點(diǎn)名稱 
    public $next; //下一節(jié)點(diǎn) 
   
    public function __construct($id, $name) { 
        $this->id = $id; 
        $this->name = $name; 
        $this->next = null; 
    } 
}

鏈表中還有兩個特別重要的方法,插入和刪除。插入需要找到插入的位置,把前一個元素的next指針指向被插入的節(jié)點(diǎn),并將被插入節(jié)點(diǎn)的next指針指向后一個節(jié)點(diǎn),如下圖左側(cè)所示。而刪除則是把前一個節(jié)點(diǎn)的next指針指向后一個節(jié)點(diǎn),并返回被刪除元素的數(shù)據(jù)內(nèi)容,如下圖右側(cè)所示。

//單鏈表 
class singelLinkList { 
    private $header; //鏈表頭節(jié)點(diǎn) 
   
    //構(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é)點(diǎn)數(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é)點(diǎn) 
    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é)點(diǎn)!<br>"; 
        } 
    }
 
    //判斷連表是否為空
    public function isEmpty(){
            return $this->header == null;
    }
 
    //清空鏈表
    public function clear(){
            $this->header = null;
    } 
 
    //獲取鏈表 
    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é)點(diǎn)名字 
    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é)點(diǎn)名稱 
    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é)點(diǎn)--------------<br>"; 
$lists->delLink ( 5 ); 
$lists->getLinkList ();
echo "<br>-----------更新節(jié)點(diǎn)名稱--------------<br>"; 
$lists->updateLink ( 3, "222222" ); 
$lists->getLinkList ();
echo "<br>-----------獲取節(jié)點(diǎn)名稱--------------<br>"; 
echo $lists->getLinkNameById ( 5 );
echo "<br>-----------獲取鏈表長度--------------<br>"; 
echo $lists->getLinkLength ();

如果你能讀到這里,小編希望你對“php如何實(shí)現(xiàn)單鏈表”這一關(guān)鍵問題有了從實(shí)踐層面最深刻的體會,具體使用情況還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想閱讀更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:php如何實(shí)現(xiàn)單鏈表
標(biāo)題鏈接:http://muchs.cn/article6/gphiog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管微信公眾號、網(wǎng)站制作、標(biāo)簽優(yōu)化、營銷型網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)