Algorithm algorithm PHP PHP queue queue

PHP queue algorithm

 

Two algorithms in this class queue is a data class, the data is located; the second queue is the queue class of this is that some operations queue.

First in the queue include front (head of the queue, that is, the team is going out of) rear (tail of the queue here forever pointing 0) queue (to store all queued data to be like, queue default there is an element of the space-time front when and he rear point) MAXSIZE (queue length) four properties

Application Note:

1 queue Initialization: generating a parameter passed in a queue as a queue maxsize initialized to 0 the rear, front queue is set to 0 at this time, only front and rear elements 0 point him

 

2. the team: whether the queue is full (front-rear == maxsize), if full of tips, Ruoguo not let the full front + 1, then let all the elements in the queue before moving an image (that is, to the new to let the position of the tail) and generates a data object into the end position +1 team. At this time, the team successfully!

3. the team: to determine whether the queue is empty (front == rear), such as tips empty, if not empty, delete the object pointed to front, front-1 (moving backward one), the team successful!

Transfer from: http: //blog.csdn.net/binbin1129/article/details/5649010

Copy the code
? <PHP 
/ ** 
* PHP queue algorithm 
*   
* the Create the On 2010-6-4 
* Author Been 
* QQ: 281 443 751 
* Email: [email protected] 
** / 
class the Data { 
    // Data 
    Private $ the Data; 
    
    public function __construct (Data $) { 
        $ this-> = $ Data Data; 
        echo $ Data. ":! brother into the team <br>"; 
    } 
    
    public function the getData () { 
        return $ this-> Data; 
    } 
    public function __destruct () { 
        . echo $ this-> the Data ": brother to go <br>!"; 
    } 
} 
class Queue { 
    protected $ Front; // team head 
    protected $ rear; // End team
    protected $ queue = array ( '0 ' => ' queue tail'); // store queue 
    public function getFrontDate () {
    protected $ maxsize; // Maximum number of 
    
    public function the __construct ($ size) { 
        $ this-> INITQ ($ size); 
    } 
    // initialize queue 
    Private function INITQ ($ size) { 
        $ this-> Front = 0; 
        $ this- > REAR = 0; 
        $ this-> MAXSIZE = $ size; 
    } 
    // null Analyzing team 
    public QIsEmpty function () { 
        return $ this-> Front == $ this-> REAR; 
    } 
    // Analyzing team full 
    public function QIsFull ( ) { 
        return ($ this-> front- $ this-> REAR) == $ this-> MAXSIZE; 
    } 
    // Get the first team data 
        return $ this-> queue [$ this- > front] -> getData (); 
    } 
    // enqueue 
    public function InQ ($ data) {
        . if ($ this-> QIsFull ( )) echo $ data ": I have to gnaw on a full (not full team into the team, please wait!) <br>!"; 
        the else { 
            $ this-> Front ++; 
            for ( I = $ this- $> Front; I $> $ this-> REAR; $ I -) { 
                // echo $ Data; 
                IF ($ this-> Queue [$ I]) the unset ($ this-> Queue [ I $]); 
                $ this-> Queue [$ I] = $ this-> Queue [$-I. 1]; 
            } 
            $ this-> Queue [$ this-> REAR +. 1] = new new Data (Data $); 
            print_r // ($ this-> Queue); 
            // echo $ this-> Front; 
            echo 'into team success! <br> '; 
        } 
    } 
    // a team 
    public function OutQ(){
        if ($ this-> QIsEmpty () ) echo " team can not empty out the team <br>!";
            this- $> front--; 
            // print_r ($ this-> Queue); 
            // echo $ this-> Front; 
            echo "the team successfully <br>!"; 
        } 
    } 
} 
$ q = new new Queue (3) ; 
$ Q-> INQ ( "seedling"); 
$ Q-> INQ ( 'Ma Shuai'); 
$ Q-> INQ ( 'skating'); 
$ Q-> INQ ( 'Zhang Shijia'); 
$ Q-> OUTQ (); 
$ q-> INQ ( "Zhou Ruixiao"); 
$ q-> OUTQ (); 
$ q-> OUTQ (); 
$ q-> OUTQ (); 
$ q-> OUTQ ();

 

Two algorithms in this class queue is a data class, the data is located; the second queue is the queue class of this is that some operations queue.

First in the queue include front (head of the queue, that is, the team is going out of) rear (tail of the queue here forever pointing 0) queue (to store all queued data to be like, queue default there is an element of the space-time front when and he rear point) MAXSIZE (queue length) four properties

Application Note:

1 queue Initialization: generating a parameter passed in a queue as a queue maxsize initialized to 0 the rear, front queue is set to 0 at this time, only front and rear elements 0 point him

 

2. the team: whether the queue is full (front-rear == maxsize), if full of tips, Ruoguo not let the full front + 1, then let all the elements in the queue before moving an image (that is, to the new to let the position of the tail) and generates a data object into the end position +1 team. At this time, the team successfully!

3. the team: to determine whether the queue is empty (front == rear), such as tips empty, if not empty, delete the object pointed to front, front-1 (moving backward one), the team successful!

Transfer from: http: //blog.csdn.net/binbin1129/article/details/5649010

Copy the code
? <PHP 
/ ** 
* PHP queue algorithm 
*   
* the Create the On 2010-6-4 
* Author Been 
* QQ: 281 443 751 
* Email: [email protected] 
** / 
class the Data { 
    // Data 
    Private $ the Data; 
    
    public function __construct (Data $) { 
        $ this-> = $ Data Data; 
        echo $ Data. ":! brother into the team <br>"; 
    } 
    
    public function the getData () { 
        return $ this-> Data; 
    } 
    public function __destruct () { 
        . echo $ this-> Data ": brother go <br>!"; 
    } 
} 
class {Queue 
    protected $ Front; // HOL 
    protected $ rear; // End team
    protected $ queue = array ( '0 ' => ' queue tail'); / / store queue 
    protected $ maxsize; // Maximum number of 
    
    public function the __construct ($ size) { 
        $ this-> INITQ ($ size); 
    } 
    // queue initialization 
    Private function INITQ ($ size) { 
        $ this-> Front = 0; 
        $ this-> REAR = 0; 
        $ this-> MAXSIZE = $ size; 
    } 
    // null Analyzing team 
    public QIsEmpty function () { 
        return $ the this -> Front == $ this-> REAR; 
    } 
    // Analyzing team full 
    public QIsFull function () { 
        return ($ this-> front- $ this-> REAR) == $ this-> MAXSIZE; 
    } 
    // Get team The first data 
    public function getFrontDate () {
        return $ this-> queue [$ this- > front] -> getData (); 
    } 
    // enqueue 
    public function InQ ($ data) {
        . if ($ this-> QIsFull ( )) echo $ data ": I have to gnaw on a full (not full team into the team, please wait!) <br>!"; 
        the else { 
            $ this-> Front ++; 
            for ( I = $ this- $> Front; I $> $ this-> REAR; $ I -) { 
                // echo $ Data; 
                IF ($ this-> Queue [$ I]) the unset ($ this-> Queue [ I $]); 
                $ this-> Queue [$ I] = $ this-> Queue [$-I. 1]; 
            } 
            $ this-> Queue [$ this-> REAR +. 1] = new new Data (Data $); 
            print_r // ($ this-> Queue); 
            // echo $ this-> Front; 
            echo 'into team success! <br> '; 
        } 
    } 
    // a team
    public function OutQ(){
        if ($ this-> QIsEmpty () ) echo "! team can not empty out the team <br>";
            this- $> front--; 
            // print_r ($ this-> Queue); 
            // echo $ this-> Front; 
            echo "the team successfully <br>!"; 
        } 
    } 
} 
$ q = new new Queue (3) ; 
$ Q-> INQ ( "seedling"); 
$ Q-> INQ ( 'Ma Shuai'); 
$ Q-> INQ ( 'skating'); 
$ Q-> INQ ( 'Zhang Shijia'); 
$ Q-> OUTQ (); 
$ q-> INQ ( "Zhou Ruixiao"); 
$ q-> OUTQ (); 
$ q-> OUTQ (); 
$ q-> OUTQ (); 
$ q-> OUTQ ();

 

Guess you like

Origin www.cnblogs.com/guiyishanren/p/11563879.html