中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

php實(shí)現(xiàn)單鏈表的實(shí)例代碼

復(fù)制代碼 代碼如下:
<?php

//鏈表節(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;
    }
}

//單鏈表
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 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ù)php實(shí)現(xiàn)單鏈表的實(shí)例代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美三级免费观看 | 成人免费黄色 | 久久99这里只有精品 | 欧美日韩精品久久久免费观看 | 中文字幕亚洲视频 | 福利片一区二区 | 自拍偷拍亚洲视频 | 国产免费xxx | 日韩免费视频一区二区 | 亚洲一区二区高清 | 黄色片免费看视频 | 国产一区二区三区免费 | 在线欧美一区二区 | 国产91亚洲精品 | 国产一级特黄真人毛片 | 91网视频| 欧美日韩精品一区二区三区四区 | 亚洲 自拍 另类 欧美 丝袜 | 亚洲自拍偷拍视频 | 国产欧美在线观看 | 狠狠操电影 | 日韩欧美在线观看一区 | 91操操操| 国产精久久久久久 | av日韩高清 | 五月婷婷激情网 | 538在线精品 | 日韩毛片在线免费观看 | 91精品国产高清一区二区三区 | 日韩av美女电影 | 久久久久亚洲av毛片大全 | 欧美日韩亚洲系列 | 黄网站在线观看 | 免费观看的黄色网址 | 亚洲不卡在线观看 | 国产精品一区二区无线 | 欧美一区二区三区在线观看 | 中文字幕一区二区三区在线观看 | 亚洲欧美日韩中文在线 | 美女黄色在线观看 | 国产精品日日摸夜夜添夜夜av |