Php bidirectional queue

Source: Internet
Author: User
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. (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.


DEQue. class. php


 _ 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?>


Demo. php


 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 order should be dbca // Example 2 $ obj = new DEQue (3, 5); // front-end only outputs, input/output at the backend, Maximum Length: 5 $ insert = array (); $ in Sert [] = $ obj-> rearAdd ('A'); $ insert [] = $ obj-> rearAdd ('B '); $ insert [] = $ obj-> frontAdd ('C'); // because the frontend can only be output, 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 falsevar_dump ($ insert); // Example 3 $ obj = new DEQue (6); // output dependency input $ obj-> frontAdd ('A '); $ obj-> frontAdd ('B'); $ obj-> frontAdd ('C'); $ obj-> rearAdd ('D'); $ result = ar Ray (); $ 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);?>



The above is the content of the php two-way queue class. For more information, see The PHP Chinese network (www.php1.cn )!

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.