A queue is a special linear table that allows only the front end of the table to be deleted. On the back end of the table, you can call it REAR for insertion. A queue, like a stack, is a linear table with limited operations. Unlike a stack, a queue follows a first-in, first-out rule, while a stack follows a first-in, last-out rule. The end of the queue where the insertion operation is performed is called the end of the queue, and the end of the queue where the deletion operation is performed is called the end of the queue. The insertion operation can be performed only at the end of the queue, and the deletion operation can be performed at the end of the queue.

The data elements of a queue are also called queue elements. Inserting an element at the end of a queue is called joining, and deleting an element at the head of a queue is called leaving. Specific implementation reference code:

Code:


      
/** ** PHP queue algorithm * Author Been * QQ:281443751 * Email:[email protected] **/
class data {
  / / data
  private $data;

  public function __construct($data){
    $this->data=$data;
    echo $data."I'm in the team! 

"
; } public function getData(){ return $this->data; } public function __destruct(){ echo $this->data."Brother is gone!

"
; }}class queue{ protected $front;/ / team head protected $rear;/ / of the protected $queue=array('0'= >'the line');// Store queues 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; } // The queue is empty public function QIsEmpty(){ return $this->front==$this->rear; } // The queue is full public function QIsFull(){ return ($this->front-$this->rear)==$this->maxsize; } // Get queue leader data public function getFrontDate(){ return $this->queue[$this->front]->getData(); } / / team public function InQ($data){ if($this->QIsFull())echo $data.": I am full as soon as I come! (Please wait if the queue is full.)

"
; 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 'Made the team!

'
; }}/ / out of the team public function OutQ(){ if($this->QIsEmpty())echo "The team can't leave the team!

"
; else{ unset($this->queue[$this->front]); $this->front--; //print_r($this->queue); //echo $this->front; echo "Team out!

"
; }}}$q=new queue(3); $q->InQ("Sri Lanka"); $q->InQ('Ma Shuai'); $q->InQ('ice skating); $q->InQ('Zhang Shijia'); $q->OutQ(); $q->InQ("Zhou Ruixiao"); $q->OutQ(); $q->OutQ(); $q->OutQ(); $q->OutQ(); Copy the code

There are two classes in this case:

The first is the data class, used to realize the storage of data and queue elements in the queue out of the queue;

The second is the Queue class, which is used for some in-queue and out-queue operations on queue elements.

The queue contains four attributes:

Front (head of queue)

Rear (the end of the queue)

Maxsize (the length of the queue, i.e. the number of queue elements)

Queue (objects that hold all queued elements)

Scenario Description:

1. To initialize the queue, create a queue, pass a parameter as maxsize to initialize the queue and set rear to 0 and front to 0. There is only element 0 in the queue, and both rear and front point to it.

2. Before joining a queue, check whether the queue is full (front-rear == maxsize). If the queue is full, the queue cannot be inserted; if it is not, the queue can be inserted. When inserting, front increments, moves all elements of the queue forward one bit in turn (giving way to the end of the queue for new elements to be inserted), and generates a new data object to be inserted at the end of the queue.

3. When exiting the queue, check whether the queue is empty (front == rear). If the queue is empty, the queue cannot exit. If it is not empty, delete the object to which front points and decrement to finish queuing.

The running results are as follows:

Miao: I'm in the team! I made the team! Team success skating: brother into the team! Enter a team success zhang Shijia: I come zha full! (Please wait if the queue is full.) Miao: I'm gone! Team out! Zhou Ruixiao: brother is in the team! Ma Shuai: I'm gone! Team out! Skating: the elder brother walked! Team out! Zhou Ruixiao: Brother is gone! Team out! We can't leave the team! We can't leave the team!Copy the code

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you