The detailed steps of implementing queue in php

What is a queue?

Queue is a kind of special linear table. It only allows deletion at the front end of the table, which can be called front, and insertion at the back end of the table, which can be called rear.

Like stack, queue is a linear table with limited operation. The difference between queue and stack is that queue follows the principle of "first in, first out", while stack follows the principle of "first in, then out". The end of the queue for insertion is called the end of the queue, and the end of the queue for deletion is called the head of the queue. Only insertion at the end of the queue and deletion at the head of the queue are allowed.

The data element of the queue is also called the queue element. Inserting an element at the end of the queue is called queue entry, and deleting an element at the head of the queue is called queue exit.

Implementation code:

* php Queue algorithm
class data {
  private $data;
  public function __construct($data){
    echo $data.":Brother is in the team!<br>";
  public function getData(){
    return $this->data;
  public function __destruct(){
    echo $this->data.": Brother is gone!<br>";
class queue{
  protected $front;//Team leader
  protected $rear;//Team tail
  protected $queue=array('0'=>'Team tail');//Storage queue
  protected $maxsize;//Maximum number
  public function __construct($size){
  //Initialize queue
  private function initQ($size){
  //Judge team empty
  public function QIsEmpty(){
    return $this->front==$this->rear;
  //Judgement team
  public function QIsFull(){
    return ($this->front-$this->rear)==$this->maxsize;
  //Get team leader data
  public function getFrontDate(){
    return $this->queue[$this->front]->getData();
  //Join the team
  public function InQ($data){
    if($this->QIsFull())echo $data.":How come I'm full! (the team is full, please wait!)<br>";
    else {
        //echo $data;
      $this->queue[$this->rear+1]=new data($data);
      //echo $this->front;
      echo 'Team success!<br>';
  //Team out
  public function OutQ(){
    if($this->QIsEmpty())echo "You can't leave the team when the team is empty!<br>";
      //echo $this->front;
      echo "Team out success!<br>";
$q=new queue(3);
$q->InQ("Young seedling");
$q->InQ('Zhang Shijia');
$q->InQ("Zhou Lei Xiao");

There are two categories in this case:

The first is data class, which is used to store data and queue elements;

The second is the queue class, which is used for some queuing operations of queue elements.

There are four properties in the queue:

Front (the head of the queue)

Rear (end of queue)

Maxsize (the length of the queue, that is, the number of queue elements)

Queue (object holding all queued elements)

Scenario Description:

1. When initializing the queue, a queue is generated, and a parameter is passed in as maxsize initialization queue to set the rear of the queue to 0, and the front of the queue head to 0. At this time, there is only element 0 in the queue, and both the rear and front point to it.

2. When entering a queue, you need to first determine whether the queue is full (front rear = = maxsize). If it is full, you cannot insert it. If it is not, you can insert it. When inserting, the front automatically increases, and then moves all elements of the queue forward one bit in turn (giving way to the end of the queue to insert new elements), and then generates a new data object to insert into the end of the queue.

3. When leaving the queue, judge whether the queue is empty (front == rear). If it is empty, you cannot leave the queue. If it is not empty, delete the object pointed to by the front, and the front will reduce itself to complete the team leaving.

The operation results are as follows:

Tags: Programming PHP

Posted on Thu, 09 Jan 2020 21:57:13 -0800 by irishprogrammin