About PHP two-way Queue class explanation

Source: Internet
Author: User
(deque, full name double-ended queue) is a data structure that has the nature of queues and stacks. Elements in a two-way queue can be ejected from both ends, and their qualified insert and delete operations are performed at both ends of the table.

In practice, you can also have output-constrained bidirectional queues (that is, one endpoint allows insertions and deletions, the other endpoint allows only two bidirectional queues to be inserted) and a restricted bidirectional queue (that is, one endpoint allows insertions and deletions, and the other endpoint allows only two-way queues to be deleted). If an element that qualifies a bidirectional queue to be inserted from an endpoint can only be removed from that endpoint, the bidirectional queue is transformed into a stack of two stacks that are adjacent to the bottom of the stack.

DEQue.class.php

<?php/** php bidirectional queue.     Support limit queue Length, input restricted, output limited, and output must be in the same port with the input several settings * date:2014-04-30* author:fdipzone* ver:1.0** func:* public Frontadd Front into row * Public frontremove front-end dequeue * Public Rearadd backend into row * pulbic Rearremove Backend dequeue * Public clear empty Pair Column * Public isfull determines whether the column is full * Private getlength gets the column length * Private setaddnum entry column, output dependent input call * Private Setrem    Ovenum record dequeue, output dependent input when called * private checkremove Check output dependent input */class deque{//class start Private $_queue = Array ();//Pair column   Private $_maxlength = 0;        For the maximum column length, 0 means no limit to private $_type = 0;    To column type private $_frontnum = 0;     Number of front-end inserts private $_rearnum = 0; Number of back-end inserts/** Initialize * @param $type to column type * 1: Both ends can be input and output * 2: Front-end can only be entered, the backend may be Input/Output * 3: Front-end only output, back-end can be input and output * 4: Back-end can only input, front-end can be input and output * 5: Back-end only output, front-end  Input/output * 6: Both ends can be input and output, at which end the input can only output from which side * @param $maxlength to the maximum length of the column * *  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 into row * @param Mixed $data 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;            /** Front-end dequeue * @return Array */Public Function Frontremove () {if ($this->_type==2) {//front-end output limit        return null;        } if (! $this->checkremove (1)) {//check whether the input return null is dependent;        } $data = null;            if ($this->getlength () >0) {$data = Array_shift ($this->_queue);        $this->setremovenum (1);    } return $data; }/** back end into row * @parAM Mixed $data Data * @return Boolean */Public function Rearadd ($data =null) {if ($this->_type==5) {//        Back-end input limit return false;            } if (Isset ($data) &&! $this->isfull ()) {Array_push ($this->_queue, $data);            $this->setaddnum (2);        return true;    } return false;            }/** back-end dequeue * @return Array */Public Function Rearremove () {if ($this->_type==4) {//back-end output limit        return null;        } if (! $this->checkremove (2)) {//check whether the input return null is dependent;        } $data = null;            if ($this->getlength () >0) {$data = Array_pop ($this->_queue);        $this->setremovenum (2);    } return $data;        /** Empty the 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->_maxlength!=0 && $this->_maxlength== $this->getlength ()) {$bIsFull = true;    } return $bIsFull;    /** gets the current pair of column lengths * @return int */Private Function GetLength () {return count ($this->_queue);        }/** input column, call * @param int $endpoint endpoint 1:front 2:rear */Private Function Setaddnum ($endpoint) when output dependent inputs {            if ($this->_type==6) {if ($endpoint ==1) {$this->_frontnum + +;            }else{$this->_rearnum + +; }}}/** records a dequeue, the output depends on input when called * @param int $endpoint endpoint 1:front 2:rear */Private Function Setremovenum ($            Endpoint) {if ($this->_type==6) {if ($endpoint ==1) {$this->_frontnum--;            }else{$this->_rearnum--; }}}/** check if output dependent input * @param int $endpoint endpoint 1:front 2:rear */PrivaTe function Checkremove ($endpoint) {if ($this->_type==6) {if ($endpoint ==1) {return $thi            s->_frontnum>0;            }else{return $this->_rearnum>0;    }} return true; }}//Class end?>

demo.php

<?phprequire "DEQue.class.php";//Example 1$obj = new DEQue (); Both front and rear end can be input, infinite length $obj->frontadd (' a ');  Front end into row $obj->rearadd (' B '); Back end into row $obj->frontadd (' C ');  Front end into row $obj->rearadd (' d '); After the backend into row//into row the array should be Cabd$result = Array (); $result [] = $obj->rearremove (); Back-end dequeue $result[] = $obj->rearremove (); Back-end dequeue $result[] = $obj->frontremove (); Front-end dequeue $result[] = $obj->frontremove (); Front-end dequeue Print_r ($result); The dequeue order should be dbca//example 2$obj = new DEQue (3, 5); The front end can only output, the back end is input and output, the maximum length 5$insert = Array (), $insert [] = $obj->rearadd (' a '), $insert [] = $obj->rearadd (' B '); $insert [ ] = $obj->frontadd (' C '); Because the front end can only output, this returns false$insert[] = $obj->rearadd (' d '), $insert [] = $obj->rearadd (' e '); $insert [] = $obj Rearadd (' f '); $insert [] = $obj->rearadd (' G '); Over length, return falsevar_dump ($insert);//Example 3$obj = new DEQue (6); Output dependent input $obj->frontadd (' a '), $obj->frontadd (' B '), $obj->frontadd (' C '); $obj->rearadd (' d '); $result = Array (); $result [] = $obj->rearremove (); $result [] = $obj-&Gt;rearremove (); Because the output depends on the input, this returns null$result[] = $obj->frontremove (); $result [] = $obj->frontremove (); $result [] = $obj Frontremove (); Var_dump ($result);? >

This article explains the PHP bidirectional queue class, more relevant content please focus on PHP Chinese web.

Related recommendations:

The difference between PHP Heredoc and Nowdoc and its characteristics

About the difference between HTML5 Localstorage and Sessionstorage

About the php zip file content Comparison class explanation

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.