資料結構之隊列——順序儲存結構(php代碼實現——方法三)

來源:互聯網
上載者:User

標籤:資料結構 隊列 php

<?php/** * 第三種——迴圈順序隊列的實現方法 * 此方法是解決前兩種方法的缺點,利用迴圈隊列的方法達到了最優時間複雜度和空間複雜度 * * */class SqQueue3{    const ARR_MAX=20;    private $SqArr;    private $front;    private $rear;    //初始化隊列    public function __construct(){        $this->SqArr=array();        $this->front=0;        $this->rear=0;    }    //銷毀隊列    public function DestroyQueue(){        $this->SqArr=null;        $this->front=$this->rear=0;    }    //清空隊列    public function ClearQueue(){        $this->SqArr=array();        $this->front=$this->rear=0;    }    //隊列是否為空白    public function QueueEmpty(){        if($this->front==$this->rear){            return ‘Null‘;        }else{            return ‘No Null‘;        }    }    //隊列的長度    public function QueueLength(){        return ($this->rear - $this->front + self::ARR_MAX) % self::ARR_MAX;    }    //取得隊頭元素    public function GetHead(){        if($this->rear==$this->front){            return ‘ERROR‘;        }        return $this->SqArr[$this->front];    }    //從隊尾摻入元素    public function EnQueue($elem){        $tail=($this->rear + 1)%self::ARR_MAX;//如果此值等於頭元素說明隊列已滿        if($tail == $this->front){            return ‘ERROR‘;        }        $this->SqArr[$this->rear]=$elem;        $this->rear=($this->rear+1)%self::ARR_MAX;        return ‘OK‘;    }    //從隊頭刪除元素    public function DeQueue(){        if($this->rear==$this->front){            return ‘ERROR‘;        }        unset($this->SqArr[$this->front]);        $this->front=($this->front + 1)%self::ARR_MAX;        return ‘OK‘;    }    //遍曆隊元素    public function QueueTraverse(){        $arr=array();        for($i=0;$i<self::ARR_MAX;$i++){            if(isset($this->SqArr[$i])){                $arr[]=$this->SqArr[$i];            }        }        return $arr;    }    //或者    public function QueueTraverse2(){        $arr=array();        $i=$this->front;        while($i != $this->rear){            $arr[]=$this->SqArr[$i];            $i=($i+1)%self::ARR_MAX;        }        return $arr;    }}


本文出自 “一切皆有可能” 部落格,請務必保留此出處http://noican.blog.51cto.com/4081966/1600887

資料結構之隊列——順序儲存結構(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.