畫江湖之資料結構【第一話:連結串列】單向連結串列

Krisji發表於2019-03-19

1 單連結串列

概括:單連結串列有一個引用指向後續節點
file
請看上圖:data為資料,next為指向下一個節點的引用

完整程式碼塊

<?php 

class Node{
    public $data = null;
    public $next = null;
    public function __construct($data,$next=null){
        $this->data = $data;
        $this->next = $next;
    }
}

class singleLinkList{
    private $header = null;
    private $last = null;
    public $size = 0;

    public function __construct(){

    }

    public function add($data){
        $node = new Node($data);
        if($this->header == null and $this->last==null){
            $this->header = $node;
            $this->last = $node;
        }else{
            $this->last->next = $node;
            $this->last = $node;
        }
    }

    public function del($data){
        $node=$this->header;
        if($node->data == $data){
            $this->header = $this->header->next;
            return true;
        }else{
            while($node->next->data == $data){
                $node->next = $node->next->next;
                return true;
            }
        }
        return false;
    }

    public function update($old,$new){
        $node = $this->header;
        while($node->next != null){
            if($node->data == $old){
                $node->data = $new;
                return true;
            }
            $node = $node->next;
        }
        echo "not found!";
        return false;
    }

    public function find($data){
        $node = $this->header;
        if  ($node->data  ==  $data)  {
            echo  'found!';
            return  true;
        }
        while($node->next != null){
            if($node->data == $data){
                echo "found!";
                return;
            }
            $node = $node->next;
        }
        echo "not found!";
    }

    public function getAll(){
        $node = $this->header;
        while($node->next != null){
            echo $node->data;
            $node = $node->next;
        }
        echo $node->data;
    }
}

$list =  new singleLinkList();
$list->add("1");
$list->add("2");
$list->add("3");
$list->add("4");
$list->add("5");
$list->add("6");
echo "<pre>";
// $list->getAll();

// if($list->del("2")){
//  echo "success";
// }else{
//  echo "false";
// }

// if($list->update("3","7")){
//  var_dump($list);
// }

$list->find(7);
// $list->getAll();

2 分析程式碼塊 具體分析到程式碼註釋哦 ~

新增節點

public function add($data){
        //申明一個節點物件
        $node = new Node($data);
        //如果頭部和尾部為null
        if($this->header == null and $this->last==null){
            $this->header = $node;//插入第一個資料到頭部
            $this->last = $node;//插入第一個資料到尾部
        }
        //如果頭部和尾部不為null 也就是第二次插入節點的時候
        else{
            $this->last->next = $node;//把尾部的下一個節點為當前插入的節點
            $this->last = $node;//把尾部的節點替換成當前插入的節點
        }
    }
$list =  new singleLinkList();
$list->add("1");
$list->add("2");
$list->add("3");
$list->add("4");
$list->add("5");
$list->add("6");

迴圈節點

public function getAll(){
        //頭部節點定義
        $node = $this->header;
        //迴圈 如果該節點有下個子節點就開始輸出
        while($node->next != null){
            echo $node->data;//輸出當前節點的資料
            $node = $node->next;//把下個子節點換作到當前節點
        }
        echo $node->data;//最後輸出尾部節點資料
    }

刪除節點

public function del($data){
        $node=$this->header;//定義所有的節點
        if($node->data == $data){ //如果要刪除的節點是頭部節點
            $this->header = $this->header->next;//就把頭部的節點移動到頭部節點的下一個子節點
            return true;
        }else{
            while($node->next->data == $data){//迴圈節點:刪除的節點和當前節點的下一個節點相同時
                $node->next = $node->next->next;//就把要當前的這個節點的下一個節點 指向下一個下一個的節點
                return true;
            }
        }
        return false;
    }
  $list->del("2")

修改節點

 public function update($old,$new){
        $node = $this->header;//先定義頭部節點
        while($node->next != null){//如果有下一個節點
            if($node->data == $old){//當前遍歷的節點的資料等於你要刪除的資料的時候
                $node->data = $new;//替換成新的資料
                return true;
            }
            $node = $node->next;//當前的節點為下一個節點 作用:迴圈下一個連結串列
        }
        echo "not found!";//沒找到就沒了
        return false;
    }
    $list->update("3","7")
 }

查詢節點

public function find($data){
$node = $this->header;//先申明一個頭部節點
while($node->next != null){//遍歷下一個節點
if($node->data == $data){//如果你要查詢的資料等於當前的節點的資料的時候 算找到了
echo "found!";
return;
}
$node = $node->next;
}
echo "not found!";
}
$list->find(7);

相關文章