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

Source: Internet
Author: User

<?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)

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.