Data Structure instant-php implementation queue, instant-php queue
Queue: meets the first-in-first-out (FIFO) Rules;
The following uses php to implement a simple cyclic queue model;
The queue in the initial status. The queue length is 0. The pointer of the team head and the team end is the same as that of the queue;
Team entry operation: Move the pointer at the end of the team backward and add one to the length;
Team-out operation: the head pointer of the team moves backward and the length is reduced by one;
Cyclic queue features: the queue size is fixed, and the memory space opened by the queue can be used cyclically. pointer movement relies on the remainder operation with queueSize;
The following example uses arrays to store queues, and array subscript is used as a pointer;
<? Php/*** Class Queue */class Queue {/*** @ var int head pointer */private $ _ front; /*** @ var int team tail pointer */private $ _ rear;/*** @ var array */private $ _ queue; /*** @ var int actual queue length */private $ _ queueLength;/*** @ var int queue capacity; */private $ _ queueSize; /*** Queue constructor. initialize the queue * @ param int $ capacity (Maximum length of the cyclic queue) */public function _ construct ($ size) {$ this-> _ queue = []; $ this-> _ queueSize = $ siz E; $ this-> _ front = 0; $ this-> _ rear = 0; $ this-> _ queueLength = 0;}/*** destroy the queue; */public function _ destruct () {unset ($ this-> _ queue );} /*** @ method: @ param mixed $ elem element * @ 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 @ 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: determines whether the queue is empty. * @ return bool */public function isEmpty () {return $ this-> _ queueLength ==== 0 ;} /*** @ method: determines whether the queue is saturated. * @ return bool */public function isFull () {return $ this-> _ queueLength ===$ this-> _ queueCapacity;}/*** @ method traverses the queue and outputs (test Queue) */public function outputQueue () {for ($ I = $ this-> _ front; $ I <$ this-> _ queueLength + $ this-> _ front; $ I ++) {echo $ this-> _ queue [$ I % $ this-> _ queueCapacity]. PHP_EOL ;}/ *** @ method clears the queue */public function clearQueue () {$ this-> _ queue = []; $ this-> _ front = 0; $ this-> _ rear = 0; $ this-> _ queueLength = 0 ;}}
There is no major problem with the test queue type. Optimization depends on real business scenarios;
$ 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); // The truth is that it fails; $ a-> outputQueue (); // output 1 2 3 echo PHP_EOL; echo $ a-> deQueue (); // output 1 Queue 2 3 echo PHP_EOL; echo $ a-> deQueue (); // output 2 queues 3 $ a-> enQueue (5); // queue 3 5 echo PHP_EOL; $ a-> outputQueue (); // output 3 5 $ a-> clearQueue (); // The queue is empty;
If any, please advise