1. 程式人生 > 實用技巧 >php版本連結串列操作

php版本連結串列操作

單鏈表

<?php
class Node{
    public $data = '';
    public $next = null;
    function __construct($data)
    {
        $this->data = $data;
    }
}


// 連結串列有幾個元素
function countNode($head){
    $cur = $head;
    $i = 0;
    while(!is_null($cur->next)){
        ++$i;
        $cur = $cur->next;
    }
    return $i;
}

// 增加節點
function addNode($head, $data){
    $cur = $head;
    while(!is_null($cur->next)){
        $cur = $cur->next;
    }
    $new = new Node($data);
    $cur->next = $new;

}

// 緊接著插在$no後
function insertNode($head, $data, $no){
    if ($no > countNode($head)){
        return false;
    }
    $cur = $head;
    $new = new Node($data);
    for($i=0; $i<$no;$i++){
        $cur = $cur->next;
    }
    $new->next = $cur->next;
    $cur->next = $new;

}

// 刪除第$no個節點
function delNode($head, $no){
    if ($no > countNode($head)){
        return false;
    }
    $cur = $head;
    for($i=0; $i<$no-1; $i++){
        $cur = $cur->next;
    }
    $cur->next = $cur->next->next;

}

// 遍歷連結串列
function showNode($head){
    $cur = $head;
    while(!is_null($cur->next)){
        $cur = $cur->next;
        echo $cur->data, '<br/>';
    }
}

$head = new Node(null);// 定義頭節點


addNode($head, 'a');
addNode($head, 'b');
addNode($head, 'c');

insertNode($head, 'd', 0);

showNode($head);

echo '<hr/>';

delNode($head, 2);

showNode($head);

  

雙向連結串列

<?php

class link
{
    public $preLink = NULL; //前指標
    public $number;         //排名
    public $name;               //名字
    public $nextLink = NULL;    //後指標

    //建構函式,初始化值
    public function __construct($number = '', $name = '')
    {
        $this->number = $number;
        $this->name = $name;
    }

    //增加資料
    public static function addDom($head, $data)
    {
        $obj = $head;
        $isExist = FALSE;

        if (NULL == $obj->nextLink) {
            $obj->nextLink = $data;
            $data->preLink = $head;
        }

        //找到新增的位置
        while (NULL != $obj->nextLink) {

            if ($obj->nextLink->number > $data->number) {
                break;

            } else if ($obj->nextLink->number == $data->number) {

                $isExist = TRUE;
                echo "<br>不能新增相同的編號";

            }

            $obj = $obj->nextLink;

        }


        if (!$isExist) {

            if (NULL != $obj->nextLink) {

                $data->nextLink = $obj->nextLink;

            }

            $data->preLink = $obj;

            if (NULL != $obj->nextLink) {

                $data->nextLink->preLink = $data;
            }

            $obj->nextLink = $data;
        }
    }


    //輸出連結串列資料
     public static function showDom($head)
    {
        $obj = $head;
        while (NULL != $obj->nextLink) {

            echo "<br>編號:" . $obj->nextLink->number . "名字:" . $obj->nextLink->name;

            $obj = $obj->nextLink;

        }
    }


    public static function delDom($head, $num)
    {
        $obj = $head;

        $isFind = FALSE;
        while (NULL != $obj) {

            if ($obj->number == $num) {

                $isFind = TRUE;

                break;

            }

            $obj = $obj->nextLink;
        }

        if ($isFind) {

            if (NULL != $obj->nextLink) {

                $obj->preLink->nextLink = $obj->nextLink;


                echo '<br/>刪除的號碼是' . $obj->number;

            } else {

                echo "<br>沒有找到目標";

            }

        }

    }


    public static function updateDom($head, $num, $name)
    {
        $obj = $head;
        while (NULL != $obj) {

            if ($obj->number == $num) {

                $obj->name = $name;

                echo "<br/>改變號碼{$obj->number}的結果:" . $obj->name;

                break;

            }

            $obj = $obj->nextLink;

        }

    }


}


$head = new link();

$one = new link(1, 'oooooooo');

$two = new link(2, 'wwwwwwww');

$three = new link(3, 'eeeeeeee');

link::addDom($head, $one);

link::addDom($head, $two);

link::addDom($head, $three);


link::showDom($head);

link::delDom($head, 2);

link::showDom($head);

link::updateDom($head, 3, 'kkkkkk');

link::showDom($head);