Queue of data structure--sequential storage structure (PHP code implementation-method three)

Source: Internet
Author: User

<?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- &GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.