_php technique of Bidirectional queue class instance implemented by PHP

Source: Internet
Author: User

This article illustrates the bidirectional queue classes implemented by PHP and its usage, and has good reference value for the learning of PHP data structure and algorithm. Share to everyone for your reference. The specific analysis is as follows:

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

The DEQue.class.php class files are as follows:

<?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 Frontad D Front End row * Public frontremove Front column * Public rearadd back end row * Pulbic Rearremove backend out * Public clear empty pair columns * PU Blic Isfull to determine whether the column is full * Private getlength get the column length * Private setaddnum record input column, output dependent input when calling * Private Setremovenum records out, Call * Private checkremove to check output dependent input/class deque{//class start Private $_queue = Array ()//To column P  Rivate $_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 output * 3: Front-End can only output, the back-end can be input and output * 4: Back-end can only input, front-end input and output * 5: Back-end can only output, front-end can be input and output * 6: Both sides can be input output, at which end input can only be output from the end * @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-End row * @param Mixed $data Data * @return Boolean/Public Function Frontadd ($data =null) {i 
    F ($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->_type==2) {//Front-End output restrictions 
    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; /** 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, $data); 
 
      $this->setaddnum (2); 
    return true; 
  return false; 
      /** Back-end column * @return Array */Public Function Rearremove () {if ($this->_type==4) {//back-end output restrictions 
    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 = array (); 
    $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; 
  /** Gets the current column length * @return int/private Function getlength () {return count ($this->_queue); 
    /** input column, output dependent inputs called * @param int $endpoint endpoint 1:front 2:rear/Private Function Setaddnum ($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/Private 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 ($ENDPO 
      int) {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 can be input, infinite length $obj->frontadd (' a '); Front End Row $obj->rearadd (' B '); Back-end row $obj->frontadd (' C '); Front End Row $obj->rearadd (' d '); 
 
After the back-end row//row array should be cabd $result = array (); $result [] = $obj->rearremove (); The back end $result [] = $obj->rearremove (); The back end $result [] = $obj->frontremove (); Front end $result [] = $obj->frontremove (); Front column print_r ($result); The column order should be DBCA//Example 2 $obj = new DEQue (3, 5); 
The front-end can only output, the back end can be input 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 '); 
 
exceeds the length, returns 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 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 the site.

I hope this article will help you with the learning of algorithmic design of PHP program.

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.