Bidirectional Queue class for PHP

Source: Internet
Author: User

(Deque, the 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 qualifying insert and delete operations are performed at both ends of the table.

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

DEQue.class.php

<?php/** php bidirectional queue.   Support for qualifying queue length, input restricted, output restricted, and output must have several settings with input * date:2014-04-30 * author:fdipzone * ver:1.0 * * Func: * Public Frontadd Front-End Row * Public frontremove Front column * Public rearadd backend row * pulbic Rearremove back-end column * Publ IC Clear clears the column * public isfull to determine whether the column is full * Private getlength get the column length * Private setaddnum record input column, lose When dependent input is invoked * Private setremovenum record column, output dependent input call * Private Checkremove check whether output dependent input/class deque{//Class St Art Private $_queue = Array ();   To the column private $_maxlength = 0;        For the maximum length of the column, 0 indicates no limit to private $_type = 0;    To the column type private $_frontnum = 0;     Number of front end inserts private $_rearnum = 0;                    Number of backend inserts/** * @param $type to column type * 1: Both ends can be input and output *                    2: Front-End can only input, back-end can be input and output * 3: Front-End can only output, back-end can be input and output * 4: Back-end can only input, the front-end can be input output * 5: The back end can only output, front-end can be input output * View this column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/* 6: Both ends can be input output, at which end input Only from which end can output * @param $maxlength The maximum length of the column/Public function __construct ($type =1, $maxlength =0) {$this-& Gt;_type = In_array ($type, Array (1,2,3,4,5,6))?  
        $type: 1;  
    $this->_maxlength = intval ($maxlength); /** Front-End 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 column * @return Array */Public Function Frontremove () {if ($this-&  
 gt;_type==2) {//Front end output limit           return null;  
        } if (! $this->checkremove (1)) {//check whether to rely on input return null;  
      
        } $data = null;  
      
            if ($this->getlength () >0) {$data = Array_shift ($this->_queue);  
      
        $this->setremovenum (1);  
      
    return $data; /** the back-end 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, $dat  
      
            a);  
      
            $this->setaddnum (2);  
      
        return true;  
    return false; /** Back-end column * @return Array/Public Function Rearremove () {if ($this-&G T;_type==4) {//back-end output limit return null;  
        } if (! $this->checkremove (2)) {//check whether to rely on input return null;  
      
        } $data = null;  
      
            if ($this->getlength () >0) {$data = Array_pop ($this->_queue);  
      
        $this->setremovenum (2);  
      
    return $data; /** Empty pairs of columns * @return Boolean/Public Function clear () {$this->_queue = ar  
        Ray ();  
        $this->_frontnum = 0;  
        $this->_rearnum = 0;  
    return true; /** to determine if the column is full * @return Boolean/Public Function Isfull () {$bIsFull = False  
        ;  
        if ($this->_maxlength!=0 && $this->_maxlength== $this->getlength ()) {$bIsFull = true;  
    return $bIsFull; /** get current pair of column length * @return int * */Private FUNction GetLength () {return count ($this->_queue); /** the input column, output dependent inputs to call * @param int $endpoint endpoint 1:front 2:rear/Private Function Setadd Num ($endpoint) {if ($this->_type==6) {if ($endpoint ==1) {$this->_frontnum + +  
            ;  
            }else{$this->_rearnum + +; /** log column, output dependent input call * @param int $endpoint endpoint 1:front 2:rear/pri  
                vate function Setremovenum ($endpoint) {if ($this->_type==6) {if ($endpoint ==1) {  
            $this->_frontnum--;  
            }else{$this->_rearnum--;  /** Check to see if output dependent input * @param int $endpoint endpoint 1:front 2:rear/private function Checkremove ($endpoint) {if ($this->_type==6) {if ($endpoint ==1) {Retu RN $this-&GT;_FRONTNUm>0;  
            }else{return $this->_rearnum>0;  
    } return true; }//Class end?>

demo.php

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.