<?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;
}
}

為東寶等地區用戶提供了全套網頁設計制作服務,及東寶網站建設行業解決方案。主營業務為做網站、
成都網站制作、東寶網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:數據結構之隊列——鏈式存儲結構(php代碼實現)-創新互聯
當前URL:http://vcdvsql.cn/article42/pgihc.html
成都網站建設公司_創新互聯,為您提供建站公司、網站策劃、動態網站、App開發、移動網站建設、網站設計
廣告
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源:
創新互聯