PHP implementation of two-way queue class instance, PHP queue Instance _php tutorial

Source: Internet
Author: User
Tags php sample code

PHP implementation of two-way queue class instance, PHP queue instance


This paper introduces the bidirectional queue class and its usage of PHP implementation, which has a good reference value for the study of PHP data structure and algorithm. Share to everyone for your reference. The specific analysis is as follows:

(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.

The DEQue.class.php class file is as follows:

<?php/** php bidirectional queue. Support limit queue Length, input restricted, output is limited, and the output must be the same as the input settings * date:2014-04-30 * author:fdipzone * ver:1.0 * Func: * Public frontadd front-end     into row * Public frontremove front-end dequeue * Public Rearadd backend into row * pulbic Rearremove Backend dequeue * Public clear empty to column * public isfull Determines whether the column is full * Private getlength gets the column length * Private setaddnum entry column, the output dependent input when called * Private setremovenum record dequeue, output dependent input when called * p Rivate checkremove Check if output dependent input */class deque{//class start Private $_queue = Array ();//To 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 input, back-end Input Output * 3: Front-end can only be lost Out, back-end can be input and output * 4: Back-end can only input, front-end can be input and output * 5: Back-end can only output, front-end can be input and output * 6: Both ends can be input and output, on which side input only from the end of the output * @param $max Length to Column max. */Public Function __construct ($type =1, $maxlength =0) {$this->_type = In_array ($type, Array ( , 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-&     gt;_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; }/** backend into row * @param Mixed $data Data * @return Boolean */Public function Rearadd ($data =null) {if ($this-&gt     ; _type==5) {//Backend 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 n     Ull     } 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); The/** input column, which is called when the output depends on the inputs * @param int $endpoint endpoint 1:front 2:rear */Private Function Setaddnum ($endpoint) {if ($t       his->_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 ($endpoin       T) {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 $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 (); Both front and rear end can be entered, infinite length $obj->frontadd (' a '); Front into row $obj->rearadd (' B '); Back end into row $obj->frontadd (' C '); Front into row $obj->rearadd (' d ');  The back end into row//into row 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 input and output, the maximum length of 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 false Var_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->rearremove (); Because the output relies on input, this returns null $result [] = $obj->frontremove (); $result [] = $obj->frontremove ();  $result [] = $obj->frontremove ();  Var_dump ($result);  ?>

Full instance code click here to download this site.

It is hoped that this article will be helpful to the study of PHP program algorithm design.


Implementing a two-way queue with PHP

The problem is unclear

Would like to write a queue class with PHP and a stack of classes, the class contains two methods, put the values and put the values 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, using PHP to achieve data structure, do not feel a little bit to eat it?

http://www.bkjia.com/PHPjc/883681.html www.bkjia.com true http://www.bkjia.com/PHPjc/883681.html techarticle PHP Implementation of two-way queue class instance, PHP Queue instance This article describes the PHP implementation of the two-way queue class and its usage, for PHP data structure and algorithm learning has a good reference value. Points ...

  • 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.