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

來源:互聯網
上載者:User

標籤:資料結構 隊列 php

<?php/** * 第二種——非迴圈順序隊列的實現方法 * 此方法,對第一種實現方法進行了最佳化,出隊時不再移動元素 * 僅僅改變頭指標的位置即可 * * 此方法的優缺點: *  優點:插入和刪除的時間複雜度都已是O(1),效率比較高 *  缺點:元素刪除後,前面的空間就無法再進行利用,造成空間的浪費 * */class SqQueue2{    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=0;        $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 GetHead(){        if($this->front == $this->rear){            return ‘ERROR‘;        }        return $this->SqArr[$this->front];    }    //隊列的長度    public function QueueLenghth(){        return $this->rear-$this->front;    }    //從隊尾插入元素    public function EnQueue($elem){        $this->SqArr[$this->rear++]=$elem;    }    //從隊頭刪除元素    public function DeQueue(){        if($this->front==$this->rear){            return ‘ERROR‘;        }        unset($this->SqArr[$this->front]);        $this->front++;        return ‘OK‘;    }    //遍曆隊列元素    public function QueueTraverse(){        $arr=array();        for($i=$this->front;$i<$this->rear;$i++){            $arr[]=$this->SqArr[$i];        }        return $arr;    }}


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

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