畫江湖之資料結構 [第二話:佇列和棧] 佇列

Krisji發表於2019-03-19

1 佇列簡介

概括:

  • 佇列是一種特殊的線性表(連結串列),特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作(FIFO),和棧一樣,佇列是一種操作受限制的線性表。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。
  • 佇列中資料元素之間的關係是一對一的關係,即除了第一個和最後一個資料元素之外,其它資料元素都是首尾相接的。
    file

完整程式碼塊

<?php 

class Node{
    public $data;
    public $next;

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

class Queue{
    public $front = null;
    public $rear = null;

    public $size = 0;

    public function __construct(){

    }

    public function push($data)
    {
        $n = new Node($data);
        if($this->front == null && $this->rear == null){
            $this->front = $n;
            $this->rear = $n;
        }else{
            $this->rear->next = $n;
            $this->rear  = $n;
        }
        $this->size++;
    }

    public function shift()
    {
        if($this->front){
            $this->size--;
            $data = $this->front->data;
            $this->front = $this->front->next;
            return $data;
        }else{
            return null;
        }
    }

}

// $s = new Queue();
// $s->push("aaaa");
// $s->push("bb");
// $s->push("cc");
// $s->push("dd");
// $s->push("fifo");

// // echo $s->shift();

// while($data = $s->shift()){
//  echo $data."<br>";
// }

//陣列實現佇列
// $arr = [];
// array_push($arr,"張三");
// array_push($arr,"李四");
// array_push($arr,"王五");
// array_push($arr,"王六");

// while($data  = array_shift($arr)){
//  echo $data."<br>";
// }

2 分析程式碼塊 具體分析到程式碼註釋哦 ~ 佇列是先進先出的結構哦

先定義一個節點

class Node{
    public $data;
    public $next;

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

壓入佇列

public $front = null;//定義頭部的變數
    public $rear = null;//定義尾部的變數

    public $size = 0;//定義佇列的總長度

    public function push($data)
    {
        $n = new Node($data);//先定義一個物件
        if($this->front == null && $this->rear == null){ //如果頭部和尾部都是為空的情況下
            $this->front = $n;//就把想壓進去的佇列初始化為頭部佇列
            $this->rear = $n;//就把想壓進去的佇列初始化為尾部佇列
        }else{
            //記住:佇列是在尾部插入 所以下面的操作是正常的。。
            $this->rear->next = $n;//第一步:尾部的下一個就是當前想插入的數值
            $this->rear  = $n;//第二步:尾部變成當前想插入的值
        }
        $this->size++;//佇列計數
    }
    // $s = new Queue();
    // $s->push("aaaa");
    // $s->push("bb");
    // $s->push("cc");
    // $s->push("dd");

出佇列

public function shift()
    {
        if($this->front){ //如果有頭部佇列 說明有值了 才可以出佇列哦
            $this->size--;//減去佇列的長度
            $data = $this->front->data;//從頭部出一個佇列的資料
            $this->front = $this->front->next;//把頭部佇列的數值 替換為他的下一個佇列
            return $data;//返回資料
        }else{
            return null;
        }
    }
     while($data = $s->shift()){
    echo $data;
 }

3 利用php的自帶的陣列實現佇列

程式碼段 和分析程式碼~

//陣列實現佇列
 $arr = [];//定義一個陣列佇列
 array_push($arr,"張三");//壓入到佇列尾部
 array_push($arr,"李四");
 array_push($arr,"王五");
 array_push($arr,"王六");

 while($data  = array_shift($arr)){//從頭部彈出一個佇列資料
    echo $data."<br>";
 }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章