相關關鍵詞
關于我們
最新文章
PHP實現(xiàn)的鏈式隊列結構示例
本文實例講述了PHP實現(xiàn)的鏈式隊列結構。分享給大家供大家參考,具體如下:
<?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('赤發(fā)鬼'); $q->outQ(); ?>