Queue of data structure--sequential storage structure (PHP code Implementation-method I)

Source: Internet
Author: User

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

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.