Sample code for a PHP data structure 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 */class queue{/** * @var int team head pointer */private $_front;    /** * @var int tail pointer */private $_rear;    /** * @var Array Queue arrays */private $_queue;    /** * @var int queue actual length */private $_queuelength;    /** * @var int queue capacity; */private $_queuesize;    /** * Queue constructor. Initialize Queue * @param int $capacity capacity (maximum circular queue Length) */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 queue * @param mixed $elem The element of the queue * @return BOOL */Public Function EnQueue ($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 Function DeQueue () {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 Function IsEmpty () {return $this->_queu    Elength = = 0; }/** * @method determine if the queue is saturated; * @return BOOL */Public Function isfull () {return $this->_queue    Length = = = $this->_queuecapacity; }/** * @method traverse queue and output (test queue) */Public Function Outputqueue () {$i = $this->_front; $i &l T $this->_queuelength + $this->_front; $i + +) {echo $this->_queue[$i% $this->_queuecapacity].        Php_eol;        }}/** * @method empty queue */Public Function Clearqueue () {$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 = new Queue (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

Related Article

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.