Two-way Queue class instance implemented in PHP

Source: Internet
Author: User
Tags mixed php class php and php sample code

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 Frontadd Front-End row

* Public Frontremove Front

* Public Rearadd back-end row

* Pulbic Rearremove back end

* Public clear clears the column

* Public Isfull to determine if the column is full

* Private GetLength get the column length

* Private Setaddnum record input column, output dependent input when called

* Private setremovenum record column, output dependent input when called

* Private Checkremove Check if output dependent input

*/

Class deque{//Class start

Private $_queue = Array (); to the column

Private $_maxlength = 0; The maximum length of the column, 0 indicates no limit

Private $_type = 0; For column types

Private $_frontnum = 0; Number of front-end Inserts

Private $_rearnum = 0; Number of back-end inserts

/** initialization

* @param $type to column types

* 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, back-end can be input output

* 4: Back end can only input, front-end can input output

* 5: Back end can only output, front-end can input output

* 6: Both ends can be input output, at which end input can only be output from which end

* @param $maxlength 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) {

if ($this->_type==3) {//Front-end input restrictions

return false;

}

if (Isset ($data) &&! $this->isfull ()) {

Array_unshift ($this->_queue, $data);

$this->setaddnum (1);

return true;

}

return false;

}

/** Front End

* @return Array

*/

Public Function Frontremove () {

if ($this->_type==2) {//front-end output limit

return null;

}

if (! $this->checkremove (1)) {//check to see if the input is dependent

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 restrictions

return false;

}

if (Isset ($data) &&! $this->isfull ()) {

Array_push ($this->_queue, $data);

$this->setaddnum (2);

return true;

}

return false;

}

/** back end

* @return Array

*/

Public Function Rearremove () {

if ($this->_type==4) {//back-end output limit

return null;

}

if (! $this->checkremove (2)) {//check to see if the input is dependent

return null;

}

$data = null;

if ($this->getlength () >0) {

$data = Array_pop ($this->_queue);

$this->setremovenum (2);

}

return $data;

}

/** Empty the columns

* @return Boolean

*/

Public Function Clear () {

$this->_queue = Array ();

$this->_frontnum = 0;

$this->_rearnum = 0;

return true;

}

/** 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 pair of column lengths

* @return int

*/

Private Function GetLength () {

Return count ($this->_queue);

}

/** record input column, output dependent inputs

* @param int $endpoint endpoint 1:front 2:rear

*/

Private Function Setaddnum ($endpoint) {

if ($this->_type==6) {

if ($endpoint ==1) {

$this->_frontnum + +;

}else{

$this->_rearnum + +;

}

}

}

/** record 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 ($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 can be input, infinite length

$obj->frontadd (' a '); Front row

$obj->rearadd (' B '); Back end Row

$obj->frontadd (' C '); Front row

$obj->rearadd (' d '); Back end Row

Row after the array should be CABD

$result = Array ();

$result [] = $obj->rearremove (); Back end out

$result [] = $obj->rearremove (); Back end out

$result [] = $obj->frontremove (); Front End

$result [] = $obj->frontremove (); Front End

Print_r ($result); The column order should be DBCA

Example 2

$obj = new DEQue (3, 5); Front end can only output, back end can input output, maximum length 5

$insert = Array ();

$insert [] = $obj->rearadd (' a ');

$insert [] = $obj->rearadd (' B ');

$insert [] = $obj->frontadd (' C '); Because the front end can only output, so this returns false

$insert [] = $obj->rearadd (' d ');

$insert [] = $obj->rearadd (' e ');

$insert [] = $obj->rearadd (' f ');

$insert [] = $obj->rearadd (' G '); exceeds 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 depends on input, this returns null

$result [] = $obj->frontremove ();

$result [] = $obj->frontremove ();

$result [] = $obj->frontremove ();

Var_dump ($result);

?>

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.