Detailed Explanation of the Implementation of PHP Queue

  • 2021-11-29 23:19:01
  • OfStack

Queue is a special linear table, which only allows deletion operation at the front end of the table, which can be called front; At the back end of the table, we can call it rear for insertion operation. Queue, like stack, is a linear table with limited operation. It is different from stack in that queue follows the principle of "first in, first out", while stack follows the principle of "first in, first out". The end of the queue for insertion operation is called the end of the queue, and the end of the queue for deletion operation is called the head of the queue. Only insertion operation is allowed at the end of the queue, and deletion operation is allowed at the head of the queue.
The data element of the queue is also called queue element. Inserting one element in the end of the queue is called queuing, and deleting one element at the head of the queue is called queuing. Specific implementation reference code:


<?php
/**
* php Queue algorithm 
* 
* Create On 2010-6-4
* Author Been
* QQ:281443751
* Email:binbin1129@126.com
**/
class data {
  // Data 
  private $data;
  
  public function __construct($data){
    $this->data=$data;
    echo $data.": I'm in the team! <br>";
  }
  
  public function getData(){
    return $this->data;
  }
  public function __destruct(){
    echo $this->data." I'm gone! <br>";
  }
}
class queue{
  protected $front;// Team leader 
  protected $rear;// End of the team 
  protected $queue=array('0'=>' End of the team ');// Storage queue 
  protected $maxsize;// Maximum number 
  
  public function __construct($size){
    $this->initQ($size);
  }
  // Initialize the queue 
  private function initQ($size){
    $this->front=0;
    $this->rear=0;
    $this->maxsize=$size;
  }
  // Judge the team empty 
  public function QIsEmpty(){
    return $this->front==$this->rear;
  }
  // Judge that the team is full 
  public function QIsFull(){
    return ($this->front-$this->rear)==$this->maxsize;
  }
  // Get team head data 
  public function getFrontDate(){
    return $this->queue[$this->front]->getData();
  }
  // Join the team 
  public function InQ($data){
    if($this->QIsFull())echo $data.": I 1 Why is it full? You can't join the team when 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 ' Success in joining the team! <br>';
    }
  }
  // Out of the team 
  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 " Get out of the team successfully! <br>";
    }
  }
}
$q=new queue(3);
$q->InQ(" Seedling ");
$q->InQ(' Ma Shuai ');
$q->InQ(' Skating ');
$q->InQ(' Zhang Shijia ');
$q->OutQ();
$q->InQ(" Zhou Ruixiao ");
$q->OutQ();
$q->OutQ();
$q->OutQ();
$q->OutQ();

There are two classes in this case:

The first is data class, which is used to realize the storage of data and the entry and exit of queue elements;

The second is the queue class, which is used for 1 queuing and dequeuing operations of queue elements.

The queue contains four attributes:

front (head of queue)

rear (tail of queue)

maxsize (length of queue, that is, number of queue elements)

queue (the object that holds all queued queue elements)

Scenario description:

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

2. When entering the queue, it is necessary to judge whether the queue is full (front-rear = = maxsize). If it is full, insertion is not allowed, and if it is not full, insertion is allowed. When inserting, front is incremented, then all elements of the queue are moved forward by one bit in turn (giving up the end of the queue to insert new elements), and then a new data object is generated and inserted into the end of the queue.

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

The running results are as follows:

Xiaomiao: Brother is in the team!
Successful entry into the team
Ma Shuai: I'm in the team!
Successful entry into the team
Skating: I'm in the team!
Successful entry into the team
Zhang Shijia: Why am I full when I come here? You can't join the team when the team is full, please wait!)
Xiaomiao: Brother is gone!
Get out of the team successfully!
Zhou Ruixiao: Brother is in the team!
Successful entry into the team
Ma Shuai: Brother is gone!
Get out of the team successfully!
Skating: I'm gone!
Get out of the team successfully!
Zhou Ruixiao: Brother is gone!
Get out of the team successfully!
You can't leave the team when the team is empty!
You can't leave the team when the team is empty!


Related articles: