Two-way queue instance implemented by php and php queue instance
This example describes the two-way queue class implemented by php and its usage. It has good reference value for learning PHP data structures and algorithms. Share it with you for your reference. The specific analysis is as follows:
(Deque, full name double-ended queue) is a data structure with queue and stack properties. Elements in a two-way queue can pop up from both ends, and the limited insert and delete operations are performed at both ends of the table.
In actual use, there can also be two-way Queues with limited output (that is, one endpoint allows insertion and deletion, and the other endpoint only allows insertion of two-way queues) and two-way queue with limited input (that is, one endpoint allows insertion and deletion, and the other endpoint only allows deletion ). If the elements inserted by a two-way queue from a certain endpoint can only be deleted from this endpoint, the two-way queue will be transformed into two adjacent stacks.
The DEQue. class. php class file is as follows:
<? Php/** php two-way queue. The queue length, input, and output must be limited. The output must be set in the same way as the input. * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public frontAdd frontend column * public frontRemove frontend column * public rearAdd backend column * pulbic rearRemove backend column * public clear column * public isFull to determine whether the column is full * private getLength to obtain the right column length * private setAddNum records into the column, * private setRemoveNum is called for output dependent input to record the columns. * private checkRemove is called for output dependent input */class DEQue {// class start pri Vate $ _ queue = array (); // pair column private $ _ maxLength = 0; // pair column maximum length. 0 indicates no limitation on private $ _ type = 0; // The column type is private $ _ frontNum = 0; // The number of front-end inserts is private $ _ rearNum = 0; // Number of backend inserts/** initialization * @ param $ type pair column type * 1: both ends can be input and output * 2: the front end can only be input, and the backend can be input and output * 3: frontend outputs only, backend inputs and outputs * 4: backend inputs only, frontend inputs and outputs * 5: backend outputs only, and frontend inputs and outputs * 6: both ends can be inputs and outputs, at which end the input can only output * @ param $ maxlength to the maximum column length */public function _ construct ($ type = 1, $ maxlength = 0) {$ this-> _ Type = in_array ($ type, array (1, 2, 3, 4, 5, 6 ))? $ Type: 1; $ this-> _ maxLength = intval ($ maxlength );} /** front-end columns * @ param Mixed $ data * @ return boolean */public function frontAdd ($ data = null) {if ($ this-> _ type = 3) {// front-end input limit return false;} if (isset ($ data )&&! $ This-> isFull () {array_unshift ($ this-> _ queue, $ data); $ this-> setAddNum (1); return true;} return false ;} /** frontend output column * @ return Array */public function frontRemove () {if ($ this-> _ type = 2) {// return null ;} if (! $ This-> checkRemove (1) {// check whether the input is dependent on return null;} $ data = null; if ($ this-> getLength ()> 0) {$ data = array_shift ($ this-> _ queue); $ this-> setRemoveNum (1) ;}return $ data ;} /** backend columns * @ param Mixed $ data * @ return boolean */public function rearAdd ($ data = null) {if ($ this-> _ type = 5) {// return false;} if (isset ($ data )&&! $ This-> isFull () {array_push ($ this-> _ queue, $ data); $ this-> setAddNum (2); return true;} return false ;} /** backend output column * @ return Array */public function rearRemove () {if ($ this-> _ type = 4) {// return null ;} if (! $ This-> checkRemove (2) {// check whether the input is dependent on return null;} $ data = null; if ($ this-> getLength ()> 0) {$ data = array_pop ($ this-> _ queue); $ this-> setRemoveNum (2);} return $ data ;} /** clear the right column * @ return boolean */public function clear () {$ this-> _ queue = array (); $ this-> _ frontNum = 0; $ this-> _ rearNum = 0; return true;}/** determines whether the column is full * @ return boolean */public function isFull () {$ bIsFull = false; if ($ this-> _ maxLeng Th! = 0 & $ this-> _ maxLength = $ this-> getLength () {$ bIsFull = true;} return $ bIsFull ;} /** get the current column length * @ return int */private function getLength () {return count ($ this-> _ queue);}/** record the column, when the output dependency is input, call * @ param int $ endpoint 1: front 2: rear */private function setAddNum ($ endpoint) {if ($ this-> _ type = 6) {if ($ endpoint = 1) {$ this-> _ frontNum ++;} else {$ this-> _ rearNum ++ ;}}/ ** records columns, call * @ param int when the output dependency is input. $ Endpoint 1: front 2: rear */private function setRemoveNum ($ endpoint) {if ($ this-> _ type = 6) {if ($ endpoint = 1) {$ this-> _ frontNum --;} else {$ this-> _ rearNum -- ;}}/** check whether the output depends on input * @ param int $ endpoint 1: front 2: rear */private function checkRemove ($ endpoint) {if ($ this-> _ type = 6) {if ($ endpoint = 1) {return $ this-> _ frontNum> 0;} else {return $ this-> _ rearNum> 0 ;}} return true ;}// class end ?>
The demo. php sample code is as follows:
<? Php require "DEQue. class. php "; // Example 1 $ obj = new DEQue (); // you can enter both the frontend and backend, with an infinite length $ obj-> frontAdd ('A '); // front-end columns $ obj-> rearAdd ('B'); // back-end columns $ obj-> frontAdd ('C '); // front-end columns $ obj-> rearAdd ('D'); // back-end columns // The array should be cabd $ result = array (); $ result [] = $ obj-> rearRemove (); // $ result [] = $ obj-> rearRemove (); // back-end columns $ result [] = $ obj-> frontRemove (); // front-end columns $ result [] = $ obj-> frontRemove (); // front-end columns print_r ($ result ); // The column sequence should be dbca // Example 2 $ obj = new DEQue (3, 5); // The front end can only be output, and the back end can be input and output, maximum length: 5 $ insert = array (); $ insert [] = $ obj-> rearAdd ('A '); $ insert [] = $ obj-> rearAdd ('B'); $ insert [] = $ obj-> frontAdd ('C'); // because the frontend can only output data, therefore, false $ insert [] = $ obj-> rearAdd ('D'); $ insert [] = $ obj-> rearAdd ('E') is returned here '); $ insert [] = $ obj-> rearAdd ('F'); $ insert [] = $ obj-> rearAdd ('G'); // exceeds the length, returns false var_dump ($ insert); // Example 3 $ obj = new DEQue (6); // The output depends on the input $ obj-> frontAdd ('A'); $ obj-> frontAdd ('B '); $ obj-> frontAdd ('C'); $ obj-> rearAdd ('D'); $ result = array (); $ result [] = $ obj-> rearRemove (); $ result [] = $ obj-> rearRemove (); // because the output depends on the input, this will return NULL $ result [] = $ obj-> frontRemove (); $ result [] = $ obj-> frontRemove (); $ result [] = $ obj-> frontRemove (); var_dump ($ result);?>
Click here to download the complete instance code.
I hope this article will help you learn PHP programming algorithm design.
Implement a two-way queue using PHP
Unknown Problem
I want to use php to write a queue class and a stack class. The class contains two methods: Put the value in and take the value out.
Class queue {
Private $ q = array ();
Public function push ($ v ){
Array_push ($ this-> q, $ v );
}
Public function shift (){
Return shift ($ this-> q );
}
}
The simplest queue.
However, is it a bit difficult to use PHP to implement the data structure?