Data structure essay-php implementation queue

Source: Internet
Author: User

Queue: A rule that satisfies FIFO;

The following uses PHP to implement a simple cyclic queue model;

The initial state of the queue, the queue length is 0, the team head and the tail of the same pointer at the beginning of the queue;

Queued operation: The tail of the team to move backward, length plus one;

Team operation: The team head pointer moves backwards, the length is reduced by one;

Cyclic queue features: The queue size fixed, the memory space opened by the queue can be recycled, the movement of the pointer is by and queuesize to take the remainder of the operation movement;

The following example is the use of arrays to implement queue storage, array subscript as a pointer;

<?PHP/** * Class Queue*/classqueue{/** * @var int team head pointer*/    Private $_front; /** * @var int tail hands*/    Private $_rear; /** * @var array queue*/    Private $_queue; /** * @var int queue actual length*/    Private $_queuelength; /** * @var int queue capacity;*/    Private $_queuesize; /** * Queue constructor. Initialize Queue * @param int $capacity Capacity (maximum length of circular queue)*/     Public function__construct ($size)    {        $this->_queue = []; $this->_queuesize =$size; $this->_front = 0; $this->_rear = 0; $this->_queuelength = 0; }    /** * Destroy queue;*/     Public function__destruct () {unset($this-_queue); }    /** * @method team * @param mixed $elem The element of the queue * @return bool*/     Public functionEnQueue ($elem)    {        if(!$this-Isfull ()) {            $this->_queue[$this->_rear] =$elem; $this->_rear++; $this->_rear =$this->_rear%$this-_queuecapacity; $this->_queuelength++; return true; }        return false; }    /** * @method out team * @return Mixed|null*/     Public functionDeQueue () {if(!$this-IsEmpty ()) {            $elem=$this->_queue[$this-_front]; //unset ($this->_queue[$this->_front]);            $this->_front++; $this->_front%=$this-_queuecapacity; $this->_queuelength--; return $elem; }        return NULL; }    /** * @method determine if the queue is empty; * @return BOOL*/     Public functionIsEmpty () {return $this->_queuelength = = 0; }    /** * @method determine if the queue is saturated; * @return BOOL*/     Public functionIsfull () {return $this->_queuelength = = =$this-_queuecapacity; }    /** * @method traverse queue and output (test queue)*/     Public functionOutputqueue () { for($i=$this->_front;$i<$this->_queuelength +$this->_front;$i++) {            Echo $this->_queue[$i%$this->_queuecapacity].Php_eol; }    }    /** * @method empty queue*/     Public functionClearqueue () {$this->_queue = []; $this->_front = 0; $this->_rear = 0; $this->_queuelength = 0; }}

Test Queue class, reasoning is no big problem, optimization depends on the real business scenario;

$a=NewQueue (3);Echo' EnQueue1 $a->enqueue (1) '.Php_eol;$a->enqueue (1);Echo' EnQueue2 $a->enqueue (2) '.Php_eol;$a->enqueue (2);Echo' EnQueue3 $a->enqueue (3) '.Php_eol;$a->enqueue (3);Echo' EnQueue4 $a->enqueue (4) '.Php_eol;$a->enqueue (4);//reasoning is a failure;$a->outputqueue ();//Output 1 2 3Echo Php_eol;Echo Php_eol;Echo $a->dequeue ();//Output 1 Queue 2 3Echo Php_eol;Echo Php_eol;Echo $a->dequeue ();//Output 2 Queue 3$a->enqueue (5);//Queue 3 5Echo Php_eol;Echo Php_eol;$a->outputqueue ();//Output 3 5$a->clearqueue ();//queue empty;

If there is no, please advise

Data structure essay-php implementation queue

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.