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 a queue is also called queue element. Inserting an element at the end of a queue is called entering a queue, and deleting an element at the head of a queue is called leaving a queue. Specific implementation reference code:
Code:
1 <?php 2 /** 3 * php Queue algorithm 4 * Author Been 5 * QQ:281443751 6 * Email:binbin1129@126.com 7 **/ 8 class data { 9 //data 10 private $data; 11 12 public function __construct($data){ 13 $this->data=$data; 14 echo $data.":Brother is in the team!<br>"; 15 } 16 17 public function getData(){ 18 return $this->data; 19 } 20 public function __destruct(){ 21 echo $this->data.": Brother is gone!<br>"; 22 } 23 } 24 class queue{ 25 protected $front;//Team leader 26 protected $rear;//Team tail 27 protected $queue=array('0'=>'Team tail');//Storage queue 28 protected $maxsize;//Maximum number 29 30 public function __construct($size){ 31 $this->initQ($size); 32 } 33 //Initialize queue 34 private function initQ($size){ 35 $this->front=0; 36 $this->rear=0; 37 $this->maxsize=$size; 38 } 39 //Judge team empty 40 public function QIsEmpty(){ 41 return $this->front==$this->rear; 42 } 43 //Judgement team 44 public function QIsFull(){ 45 return ($this->front-$this->rear)==$this->maxsize; 46 } 47 //Get team leader data 48 public function getFrontDate(){ 49 return $this->queue[$this->front]->getData(); 50 } 51 //Join the team 52 public function InQ($data){ 53 if($this->QIsFull())echo $data.":How come I'm full! (the team is full, please wait!)<br>"; 54 else { 55 $this->front++; 56 for($i=$this->front;$i>$this->rear;$i--){ 57 //echo $data; 58 if($this->queue[$i])unset($this->queue[$i]); 59 $this->queue[$i]=$this->queue[$i-1]; 60 } 61 $this->queue[$this->rear+1]=new data($data); 62 //print_r($this->queue); 63 //echo $this->front; 64 echo 'Team success!<br>'; 65 } 66 } 67 //Team out 68 public function OutQ(){ 69 if($this->QIsEmpty())echo "You can't leave the team when the team is empty!<br>"; 70 else{ 71 unset($this->queue[$this->front]); 72 $this->front--; 73 //print_r($this->queue); 74 //echo $this->front; 75 echo "Team out success!<br>"; 76 } 77 } 78 } 79 $q=new queue(3); 80 $q->InQ("Young seedling"); 81 $q->InQ('Marshuai'); 82 $q->InQ('Skating'); 83 $q->InQ('Zhang Shijia'); 84 $q->OutQ(); 85 $q->InQ("Zhou Lei Xiao"); 86 $q->OutQ(); 87 $q->OutQ(); 88 $q->OutQ(); 89 $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. A parameter is passed in as the 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 the queue, it is necessary to determine whether the queue is full (front rear = = maxsize). If it is full, it cannot be inserted, and if it is not, it can be inserted. 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:
Miao: brother is in the team! Team success Marshal: brother is in the team! Team success Skating: brother is in the team! Team success Zhang Shijia: how come I'm full! (the team is full, please wait!) Miao: brother is gone! Team out success! Zhou ruixiao: brother is in the team! Team success Marshal: brother is gone! Team out success! Skating: brother is gone! Team out success! Zhou ruixiao: brother is gone! Team out success! You can't leave the team when the team is empty! You can't leave the team when the team is empty!