<?php/** * second--non-circular sequential queue implementation method * This method, the first implementation method is optimized, the team no longer move elements * only change the position of the head pointer * * the pros and cons of this method: * advantages: The time complexity of insertions and deletions are already O (1), High efficiency * disadvantage: After the element is deleted, The space above can no longer be exploited, resulting in wasted space * */class SqQueue2{ private $SQARR; private $front;//point to Team head element private $rear;//If the queue is not empty, Then point to the next position of the tail element //initializes the variable public function __construct () { $this->sqarr=array (); $this->front=0; $this->rear=0; } //Destroy Queue public function destroyqueue () { $this->sqarr=null; $this->front=0; $this->rear=0; } //Empty queue public function clearqueue () { $ This->sqarr=array (); $this->front= $this->rear=0; } //determine if the queue is empty public function Queueempty () { if ($this->front== $this->rear) { return ' Null '; }else{ return ' No null '; } } //get team head element public function gethead () { if ($this->front == $this->rear) { return ' ERROR '; } return $this->sqarr[$this The length of the->front]; } //queue public function queuelenghth () { return $this->rear-$this->front; } //insert elements from the end of the team public function enqueue ($elem) { $this->sqarr[$this->rear++]= $elem; } //remove elements from Team head public function dequeue () { if ($ this->front== $this->rear) { return ' ERROR '; &Nbsp; } unset ($this, $this->sqarr[ Front]); $this->front++; return ' OK '; } //traversal queue element Public function queuetraverse () { $arr =array (); for ($i = $this->front; $i < $this->rear; $i + +) { $arr []= $this->sqarr[$i]; } return $arr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1600885
Queue of data structure--sequential storage structure (PHP code implementation-method two)