<?php/** * the first--non-cyclic sequential queue implementation Method * the head element of the queue is the element labeled 0 in the array * the advantages and disadvantages of this method: * Advantage: The head element is always labeled as the first element of 0 , so there is no need to set the head pointer * disadvantage: The element will move a lot of elements, the time complexity is O (n), the efficiency is relatively low * */class SqQueue{ private $SQARR;//queue storage Array private $rear;//If the queue is not empty, point to the last position of the tail element public function __construct () { $this->sqarr=array (); $ this->rear=0; } //Destruction Chain Stacks public Function destroyqueue () { $this->sqarr=null; $this->rear=0; } //Empty queue public function clearqueue () { $this->sqarr=array (); $this->rear=0; } //whether the queue is empty public function queueempty () { if ($this->rear==0) { return ' Null '; }else{ return ' No null '; } } //Length of queue public Function queuelength () { return $this->rear; } //get team head element public function gethead () { return $this->sqarr[0]; } //inserts elements from the end of the team &Nbsp; public function enqueue ($elem) { $this->sqarr[$this->rear++]= $elem; } //remove elements from Team head public function dequeue () { if ($ this->rear == 0) { return ' ERROR '; } for ($i =1; $i < $this->rear; $i +) { $this->sqarr[$i -1]= $this->sqarr[$i]; } unset ($this->sqarr[$this->rear-1]);//After the move is complete, the last element will not exist. So let it out. $this->rear--; return ' OK '; } //traversing queue elements public function Queuetraverse () { $arr =array (); for ($i =0; $i < $this->rear; $i +) { $arr []= $this->sqarr[$i]; } return $arr; return $this->sqarr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1600884
Queue of data structure--sequential storage structure (PHP code Implementation-method I)