Home > Article > Backend Development > PHP implements chained queue
This article mainly introduces the chain queue structure implemented by PHP. It analyzes the definition of PHP chain queue and the implementation and use of basic operations such as enqueue, dequeue, and print queue based on specific examples. Friends who need it can Refer to the following
The example of this article describes the chain queue structure implemented by PHP. Share it with everyone for your reference, the details are as follows:
<?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(); ?>
Running results:
The above is the detailed content of PHP implements chained queue. For more information, please follow other related articles on the PHP Chinese website!