Home > Backend Development > PHP Tutorial > PHP implements chained queue

PHP implements chained queue

巴扎黑
Release: 2023-03-16 08:58:01
Original
1153 people have browsed it

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 &#39;队满了</br>&#39;;
    } else {
      $this->tail = $node;
      $this->tail->next = $node;
      $this->len++;
      echo $node->nickname.&#39;入队成功</br>&#39;;
    }
  }
  //出队操作
  public function outQ()
  {
    if ($this->len==0)
    {
      echo &#39;队空了</br>&#39;;
    } else {
      $p = $this->front->next;
      $this->front->next = $p->next;
      $this->len--;
      echo $p->nickname.&#39;出队成功</br>&#39;;
    }
  }
  //打印队
  public function show()
  {
    for ($i=$this->len;$i>0;$i--)
    {
      $this->outQ();
    }
  }
}
echo "**********入队操作******************</br>";
$q = new queue(5);
$q->inQ(&#39;入云龙&#39;);
$q->inQ(&#39;花和尚&#39;);
$q->inQ(&#39;青面兽&#39;);
$q->inQ(&#39;行者&#39;);
$q->inQ(&#39;玉麒麟&#39;);
$q->inQ(&#39;母夜叉&#39;);
echo "**********出队队操作******************</br>";
$q->outQ();
$q->outQ();
$q->outQ();
$q->outQ();
$q->inQ(&#39;操刀鬼&#39;);
$q->inQ(&#39;截江鬼&#39;);
$q->inQ(&#39;赤发鬼&#39;);
$q->outQ();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template