Queue of data structure--chained storage structure (PHP code implementation)

Source: Internet
Author: User

<?phpclass QNode{    public   $data;    public    $next     public function __construct ($data) {          $this->data= $data;         $this->next= The null;    }}class linkqueue{ //chain queue contains the head node, which is empty when instantiated     private   $data;    private  $next;    private  $front;//point to head node     private  $rear;//pointing to the tail knot//    private  $length;     public function __construct () {         $this data=null;         $this->next=null;          $this->front= $this,  //point          $this- >rear= $this;//point to head node//         $this->length=0;    }     //Destroy Queue     public function destroyqueue () {         while ($this->front) { //destruction begins with the beginning of the first node               $this->rear= $this->front->next;             unset ($this->front);              $this->front= $this->rear;        }     }    //empty Queue     public function clearqueue () {          $p = $this->front->next;         while ($p) {             $q = $p next;             unset ($p);              $p = $q;        }          $this->front->next=null;         $this->rear=$ this->front;    }    //whether the queue is empty     public  Function queueempty () {        if ($this->front== $this->rear) {            return  ' Null ';         }else{             return  ' No null ';        }    }     //Queue Length     public function queuelength () {          $p = $this->front;         $i =0;         while ($p  !=  $this->rear) {              $i ++;             $p = $p->next;         }        return  $i;//         return  $this->length;    }     //get team head element     public function gethead () {         if ($this->front== $this->rear) {             return  ' ERROR ';        }         return  $this->front->next->data;    }     Insert Element     public function enqueue () {       from end of team    $node =new qnode (Mt_rand (100,200));         $node next= $this->rear->next;         $this->rear->next= $node;          $this->rear= $node;          $this->length++;    }    //remove elements from Team head      public function dequeue () {        if ($this->front== $this->rear) {            return  ' ERROR ';         }         $p = $this Front->next;        unset ($this->front->next);        &nBSP; $this->front->next= $p->next;        if ($this->rear== $p { //If there is only one element, then the pointer needs to change.              $this->rear= $this->front;         }         $this->length--;         return  ' OK ';    }     //Traversal queue Element     public function queuetraverse () {         if ($this->front== $this->rear) {             return  ' ERROR ';        }          $arr =array ();         $p = $this->front- >next;        while ($p) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;       $arr []= $p->data;              $p = $p->next;        }         return  $arr;     }}


This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1600889

Queue of data structure--chained storage structure (PHP code implementation)

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.