时间:2021-07-01 10:21:17 帮助过:3人阅读
本文实例讲述了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实现链式队列的详细内容,更多请关注Gxl网其它相关文章!