淺談PHP資料結構之單鏈表

來源:互聯網
上載者:User

標籤:water   ack   版本號碼   儲存   插入   資料結構   public   gravity   檢查   

什麼是鏈表?(依據百度詞條查詢而得)

鏈表是一種實體儲存體單元上非連續、非順序的儲存結構,資料元素的邏輯順序是通過鏈表中的指標連結次序實現的。鏈表由一系列結點(鏈表中每個元素稱為結點)組成,結點能夠在執行時動態產生。每個結點包含兩個部分:一個是儲存資料元素的資料域,還有一個是儲存下一個結點地址的指標域。

經過查詢資料和觀摩了網站不同版本號碼的鏈表後。小弟自己嘗試著寫了一個PHP版本號碼的單鏈表,希望對大家理解鏈表有所協助。

<?php
//定義結點
class Node{
    public $id;//結點ID
    public $data;//結點資料
    public $next;//指向下一結點
    public function __construct($id,$data){
        $this->id=$id;
        $this->data=$data;
        $this->next=NULL;
    }
}
class Linklist{
    private $head;//定義頭結點
    public function __construct($id=0,$data=NULL){
        $this->head=new Node($id,$data);
    }
    //插入結點
    public function addNode($node){
        $head=$this->head;
        //插入之前檢查ID是否衝突
        if($this->getNode($node->id)!=false){
            echo "您要插入的ID為".$node->id."資料為".$node->data."的結點已經存在該ID。請改動ID後重試<br>";
            return false;
        }
        while($head->next!=NULL){
            if($head->next->id >= $node->id){
                break;
            }
            $head=$head->next;
        }
        $node->next=$head->next;
        $head->next=$node;

    }

    //刪除結點
    public function delNode($id){
        $head=$this->head;
        $temp=false;
        //注意。刪除操作須要找到須要刪除的前一個結點
        while($head->next != NULL){
            if($head->next->id==$id){
                $temp=true;
                break;
            }
            $head=$head->next;
        }
        if($temp==true){
            if($head->next->next==null){
                $head->next=NULL;
            }
            else
                $head->next=$head->next->next;
            
        return true;
    }
    else {
        echo ‘未找到id為‘.$id.‘的結點<br>‘;
    }
    }

    //擷取鏈表
    public  function getList(){
        $head=$this->head;
        if($head->next==NULL){
            echo ‘該鏈表為空白‘;
        }
        while($head->next !=NULL){
            if($head->id != 0)
            echo ‘ID為‘.$head->id.‘的結點中的資料為‘.$head->data.‘<br/>‘;
            $head=$head->next;
        }
            echo ‘ID為‘.$head->id.‘的結點中的資料為‘.$head->data;
    }

    //擷取結點
    public function getNode($id){
        $head=$this->head;
        while($head !=NULL){
            if($head->id==$id)
                return $head->data;
            $head=$head->next;
        }
        return false;
    }

    //擷取長度
    public function getLength(){
        $head=$this->head;
        $num=0;
        while($head->next !=NULL){
            $num++;
            $head=$head->next;
        }
        return $num;
    }
}
$a=new Linklist;
$a->addNode(new Node(1,"Hello Word"));
$a->addNode(new Node(2,3));
$a->addNode(new Node(2,11123));
$a->addNode(new Node(3,11123));
$a->getList();
$b=$a->getNode(3);
$c=$a->getLength();
var_dump($b);
var_dump($c);
?>

最後為測試代碼,以下是測試結果:

小弟初學。哪裡不正確的希望大家能夠包容下。給點建議。


淺談PHP資料結構之單鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.