您好,登錄后才能下訂單哦!
<?php /** * 第二種——非循環(huán)順序隊(duì)列的實(shí)現(xiàn)方法 * 此方法,對(duì)第一種實(shí)現(xiàn)方法進(jìn)行了優(yōu)化,出隊(duì)時(shí)不再移動(dòng)元素 * 僅僅改變頭指針的位置即可 * * 此方法的優(yōu)缺點(diǎn): * 優(yōu)點(diǎn):插入和刪除的時(shí)間復(fù)雜度都已是O(1),效率比較高 * 缺點(diǎn):元素刪除后,前面的空間就無(wú)法再進(jìn)行利用,造成空間的浪費(fèi) * */ class SqQueue2{ private $SqArr; private $front;//指向隊(duì)頭元素 private $rear;//如果隊(duì)列不為空,則指向隊(duì)尾元素的下一個(gè)位置 //對(duì)變量進(jìn)行初始化 public function __construct(){ $this->SqArr=array(); $this->front=0; $this->rear=0; } //銷(xiāo)毀隊(duì)列 public function DestroyQueue(){ $this->SqArr=null; $this->front=0; $this->rear=0; } //清空隊(duì)列 public function ClearQueue(){ $this->SqArr=array(); $this->front=$this->rear=0; } //判斷隊(duì)列是否為空 public function QueueEmpty(){ if($this->front==$this->rear){ return 'Null'; }else{ return 'No Null'; } } //取得隊(duì)頭元素 public function GetHead(){ if($this->front == $this->rear){ return 'ERROR'; } return $this->SqArr[$this->front]; } //隊(duì)列的長(zhǎng)度 public function QueueLenghth(){ return $this->rear-$this->front; } //從隊(duì)尾插入元素 public function EnQueue($elem){ $this->SqArr[$this->rear++]=$elem; } //從隊(duì)頭刪除元素 public function DeQueue(){ if($this->front==$this->rear){ return 'ERROR'; } unset($this->SqArr[$this->front]); $this->front++; return 'OK'; } //遍歷隊(duì)列元素 public function QueueTraverse(){ $arr=array(); for($i=$this->front;$i<$this->rear;$i++){ $arr[]=$this->SqArr[$i]; } return $arr; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。