本文實例講述了PHP實現的鏈式隊列結構。分享給大家供大家參考,具體如下:
<?php header("Content-Type:text/html;charset=utf-8"); /** * 鏈式隊列 */ 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; } //入隊操作 public function inQ($nickname) { $node = new node(); $node->nickname = $nickname; if ($this->len==$this->maxSize) { echo '隊滿了</br>'; } else { $this->tail = $node; $this->tail->next = $node; $this->len++; echo $node->nickname.'入隊成功</br>'; } } //出隊操作 public function outQ() { if ($this->len==0) { echo '隊空了</br>'; } else { $p = $this->front->next; $this->front->next = $p->next; $this->len--; echo $p->nickname.'出隊成功</br>'; } } //打印隊 public function show() { for ($i=$this->len;$i>0;$i--) { $this->outQ(); } } } echo "**********入隊操作******************</br>"; $q = new queue(5); $q->inQ('入云龍'); $q->inQ('花和尚'); $q->inQ('青面獸'); $q->inQ('行者'); $q->inQ('玉麒麟'); $q->inQ('母夜叉'); echo "**********出隊隊操作******************</br>"; $q->outQ(); $q->outQ(); $q->outQ(); $q->outQ(); $q->inQ('操刀鬼'); $q->inQ('截江鬼'); $q->inQ('赤發鬼'); $q->outQ(); ?>
運行結果:
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數據結構與算法教程》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。