<?php/** * third--the implementation of cyclic sequential queue * This method is to solve the disadvantages of the first two methods, using the method of cyclic queue to achieve the optimal time complexity and spatial complexity * * */class sqqueue3{ const arr_max=20; private $SQARR; private $front; private $rear; //Initialize Queue public function __construct () { $this->sqarr=array (); $this- >front=0; $this->rear=0; } //Destroy Queue public function destroyqueue () { $this->sqarr=null; $this front= $this->rear=0; } //Empty queue public function clearqueue () {&Nbsp; $this->sqarr=array (); $this->front= $this->rear=0; } //queue is empty public function queueempty () { if ($ this->front== $this->rear) { return ' Null '; }else{ return ' no null '; } } //Length of queue public function Queuelength () { return ($this->rear - $this- >front + self::arr_max) % self::ARR_MAX; } //get team head elements public function gethead () { if ($this->rear==$ This->front) { return ' ERROR '; } return $ this->sqarr[$this->front]; } //elements from the end of the team public function enqueue ($elem) { $tail = ($this- >REAR&NBSP;+&NBSP;1)%self::arr_max;//If this value equals the header element description The queue is full if ($ tail == $this->front) { return ' ERROR '; } $this->sqarr[$this->rear]= $elem; $this Rear= ($this->rear+1)%self::arr_max; return ' OK '; } //remove element from Team Head public function dequeue () { if ($this->rear== $this->front) { return ' ERROR '; } unset ($this->sqarr[$this->front]); $this->front= ($this->front + 1)%self::arr_max; return ' OK '; } //Traversal team Elements public function queuetraverse () { $arr = Array (); for ($i =0; $i <self::ARR_MAX; $i + +) { if (Isset ($this->sqarr[$i]) { $arr []= $this->sqarr[$i]; } } return $arr; } //or public function queuetraverse2 () { $arr =array (); $i = $this->front; while ($i != $this->rear) { $arr []= $this->sqarr[$i]; $i = ($ I+1)%self::arr_max; } return $arr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1600887
Queue of data structure--sequential storage structure (PHP code implementation-method three)