您好,登錄后才能下訂單哦!
<?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; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。