由PHP實現單向鏈表引起的對象賦值,對象傳參,鏈表操做引起的一系列問題

2019年2月25日14:21:13php

測試版本php 5.4 ,5.6,7.0,7.2html

代碼請看: https://www.cnblogs.com/zx-admin/p/10373866.html測試

 

1,對象賦值this

final class Node {

    public $data;
    public $next = null;

    public function __construct($data) {
        $this->data = $data;
    }

}

$a = new Node(['a']);
p($a->data);
$b = $a;
//修改$a的data看是否影響
$a->data = 'sssss';
p($b->data);
Array
(
    [0] => a
)
sssss

會影響$a的數據spa

$b = &$a;
$b = $a;

對於對象來講就是取地址符,可是注意PHP的&不是取地址符,是別名,注意這個是官方解釋,可是注意下面的 對象傳參,又是另外一種寫法.net

 

$b = clone $a;

會裂變成連個徹底獨立的內存地址指向指針

 

2,對象傳參,或者對象傳值,對象數據內部遍歷指針的問題code

代碼htm

final class Node {

    public $data;
    public $next = null;

    public function __construct($data) {
        $this->data = $data;
    }

}

final class LinkedList {

//    //從鏈表尾部壓入一個節點,節點自動維護,不須要要像main方法那樣本身維護
    public function push(Node $head, Node $Node) {
        $current = $head; //讓$current指向$head;
        while ($current->next != null) {
            $current = $current->next;
        }
        $current->next = $Node->next;
        $current->next = $Node;
    }

    //從鏈表尾壓出一個節點
    public function pop(Node $head) {
        $current = $head; //讓$current指向$head;
        while ($current->next != null) {
            //提早查找鏈表尾部是否爲空,爲空就是尾部,吧當前節點的next複製問NULL,就是尾部元素幹掉
            if ($current->next->next == null) {
                break;
            }
            $current = $current->next;
        }
        $current->next = null;
    }

}

$head = new Node([]);
$a = new Node(['a']);
$b = new Node(['b']);
$c = new Node(['c']);
$d = new Node(['d']);

$LinkedList = new LinkedList();
$LinkedList->push($head, $a);
$LinkedList->push($head, $b);
$LinkedList->push($head, $c);
$LinkedList->push($head, $d);
$LinkedList->pop($head);
pp($head);

結果對象

Node Object
(
    [data] => Array
        (
        )

    [next] => Node Object
        (
            [data] => Array
                (
                    [0] => a
                )

            [next] => Node Object
                (
                    [data] => Array
                        (
                            [0] => b
                        )

                    [next] => Node Object
                        (
                            [data] => Array
                                (
                                    [0] => c
                                )

                            [next] => 
                        )

                )

        )

)

現階段從結果反推的是對象傳參,會自動變成引用對象傳參,就是咱們日常的寫法

function functionName(&$param) {
    
}

相似這種的效果

可是,這個還不是關鍵

這段代碼纔是關鍵

  public function push(Node $head, Node $Node) {
        $current = $head; //讓$current指向$head;
        while ($current->next != null) {
            $current = $current->next;
        }
        $current->next = $Node->next;
        $current->next = $Node;
    }

還能夠寫成這樣的

    public function push(Node $head, Node $Node) {

        while ($head->next != null) {
            $head = $head->next;
        }
        $head->next = $Node->next;
        $head->next = $Node;
    }

按照咱們正常的理解,遍歷一個對象  $head 被再次複製,那麼遍歷到最後$head->next = $Node;應該只剩一個對象元素纔對

可是打印 pp($head);依然是沒有問題是一個完整的鏈表

 

由於沒法理解這個遍歷過程  pop方法我寫了兩天沒法完成,後來是經過畫出鏈表結構才寫出來的了,同時參考了

https://blog.csdn.net/wenximalong/article/details/8296061 

可是依然沒法理解遍歷對象的爲何無論有沒有指向$head 的變量使用while遍歷的時候發生了什麼,這種結構的樹狀的對象數據,for或者foreach 固然很差使

請參看

https://www.cnblogs.com/zx-admin/p/9820866.html

若是硬要解釋的話,無論for foreach while  key next 等方法,在使用的時候都獨立維護這一套指針,爲了完成複雜的指針,指針移動完成不會影響傳入的變量或者變量對象

畫了個示意圖,不保證徹底正確,不具有給他們語言的參考性,注意!

 

 

就至關於while給你走指針,本身處理處理數據就能夠,簡單化了操做,複雜了理解,不肯定是否理解正確,若是有問題請反饋

相關文章
相關標籤/搜索