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
/**
* php Queue algorithm
**/
class data {
  //data
  private $data;
 
  public function __construct($data){
    $this->data=$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){
    $this->initQ($size);
  }
  //Initialize queue
  private function initQ($size){
    $this->front=0;
    $this->rear=0;
    $this->maxsize=$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 {
      $this->front++;
      for($i=$this->front;$i>$this->rear;$i--){
        //echo $data;
        if($this->queue[$i])unset($this->queue[$i]);
        $this->queue[$i]=$this->queue[$i-1];
      }
      $this->queue[$this->rear+1]=new data($data);
      //print_r($this->queue);
      //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>";
    else{
      unset($this->queue[$this->front]);
      $this->front--;
      //print_r($this->queue);
      //echo $this->front;
      echo "Team out success!<br>";
    }
  }
}
$q=new queue(3);
$q->InQ("Young seedling");
$q->InQ('Marshuai');
$q->InQ('Skating');
$q->InQ('Zhang Shijia');
$q->OutQ();
$q->InQ("Zhou Lei Xiao");
$q->OutQ();
$q->OutQ();
$q->OutQ();
$q->OutQ();

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