相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
PHP實(shí)現(xiàn)的鏈?zhǔn)疥?duì)列結(jié)構(gòu)示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的鏈?zhǔn)疥?duì)列結(jié)構(gòu)。分享給大家供大家參考,具體如下:
<?php header("Content-Type:text/html;charset=utf-8"); /** * 鏈?zhǔn)疥?duì)列 */ class node{ public $nickname; public $next; } class queue { public $front;//頭部 public $tail;//尾部 public $maxSize;//容量 public $next;//指針 public $len=0;//長度 public function __construct($size) { $this->init($size); } public function init($size) { $this->front = $this; $this->tail = $this; $this->maxSize = $size; } //入隊(duì)操作 public function inQ($nickname) { $node = new node(); $node->nickname = $nickname; if ($this->len==$this->maxSize) { echo '隊(duì)滿了</br>'; } else { $this->tail = $node; $this->tail->next = $node; $this->len++; echo $node->nickname.'入隊(duì)成功</br>'; } } //出隊(duì)操作 public function outQ() { if ($this->len==0) { echo '隊(duì)空了</br>'; } else { $p = $this->front->next; $this->front->next = $p->next; $this->len--; echo $p->nickname.'出隊(duì)成功</br>'; } } //打印隊(duì) public function show() { for ($i=$this->len;$i>0;$i--) { $this->outQ(); } } } echo "**********入隊(duì)操作******************</br>"; $q = new queue(5); $q->inQ('入云龍'); $q->inQ('花和尚'); $q->inQ('青面獸'); $q->inQ('行者'); $q->inQ('玉麒麟'); $q->inQ('母夜叉'); echo "**********出隊(duì)隊(duì)操作******************</br>"; $q->outQ(); $q->outQ(); $q->outQ(); $q->outQ(); $q->inQ('操刀鬼'); $q->inQ('截江鬼'); $q->inQ('赤發(fā)鬼'); $q->outQ(); ?>