激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php實現單鏈表的實例代碼

php實現單鏈表的實例代碼

2020-03-13 15:44PHP教程網 PHP教程

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 (); 
?>

 

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品黄 | 亚洲一区在线看 | 国产精品久久久久久久久久 | 久久线视频 | 久久久入口 | 久久久一区二区三区四区 | 久久久无码精品亚洲日韩按摩 | 久久久精品视频免费看 | 一色桃子av大全在线播放 | 婷婷一区二区三区四区 | 激情大乳女做爰办公室韩国 | freexxx69性欧美hd | 亚州综合图片 | 免费国产自久久久久三四区久久 | av久草 | 成人午夜免费福利 | 国产精品视频专区 | 国产精品99久久久久久久vr | 成人午夜毛片 | 欧美人一级淫片a免费播放 久久久久久久久91 国产99久久久久久免费看 | 最新一区二区三区 | 99精品视频在线观看免费 | 免费国产a | 91精品久久久久久久 | 久久久经典视频 | av电影网站在线 | 欧美中文字幕一区二区 | 久久久麻豆| 黄色电影免费提供 | 一级做受毛片免费大片 | 日韩精品中文字幕在线观看 | 操你逼 | 手机国产乱子伦精品视频 | 成人在线观看一区二区 | chinese xxxxhd videos麻豆 | 密室逃脱第一季免费观看完整在线 | 国产毛片网 | 少妇激情视频 | 九九热视频免费 | 午夜视频亚洲 | 欧美精品免费一区二区三区 |