您好,登錄后才能下訂單哦!
<?php class QNode{ public $data; public $next; public function __construct($data){ $this->data=$data; $this->next=null; } } class LinkQueue{ //鏈隊列包含頭結點,實例化時,此隊列為空 private $data; private $next; private $front;//指向頭結點 private $rear;//指向尾結點 // private $length; public function __construct(){ $this->data=null; $this->next=null; $this->front=$this; //指向頭結點 $this->rear=$this;//指向頭結點 // $this->length=0; } //銷毀隊列 public function DestroyQueue(){ while($this->front){ //銷毀首先是從頭結點開始 $this->rear=$this->front->next; unset($this->front); $this->front=$this->rear; } } //清空隊列 public function ClearQueue(){ $p=$this->front->next; while($p){ $q=$p->next; unset($p); $p=$q; } $this->front->next=null; $this->rear=$this->front; } //隊列是否為空 public function QueueEmpty(){ if($this->front==$this->rear){ return 'Null'; }else{ return 'No Null'; } } //隊列的長度 public function QueueLength(){ $p=$this->front; $i=0; while($p != $this->rear){ $i++; $p=$p->next; } return $i; // return $this->length; } //取得隊頭元素 public function GetHead(){ if($this->front==$this->rear){ return 'ERROR'; } return $this->front->next->data; } //從隊尾插入元素 public function EnQueue(){ $node=new QNode(mt_rand(100,200)); $node->next=$this->rear->next; $this->rear->next=$node; $this->rear=$node; $this->length++; } //從隊頭刪除元素 public function DeQueue(){ if($this->front==$this->rear){ return 'ERROR'; } $p=$this->front->next; unset($this->front->next); $this->front->next=$p->next; if($this->rear==$p){ //如果只有一個元素那么,為指針就需要變化了。 $this->rear=$this->front; } $this->length--; return 'OK'; } //遍歷隊列元素 public function QueueTraverse(){ if($this->front==$this->rear){ return 'ERROR'; } $arr=array(); $p=$this->front->next; while($p){ $arr[]=$p->data; $p=$p->next; } return $arr; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。