A Queue is a special first-in-first-out linear table that can only be deleted at the front end (usually called a Queue) and inserted at the backend (usually called a Queue ). The end of the delete operation is called the head of the team, and the end of the insert operation is called the end of the team. A queue organizes data according to the principle of first-in-first-out or second-out. When there are no elements in the queue, it is called an empty queue.
Data Structure and algorithm (implemented in PHP)-Queue 1
<? Php
/**
* Data structures and algorithms (implemented in PHP)-Queue ).
*
* @ Author creative programming (TOPPHP. ORG)
* @ Copyright Copyright (c) 2013 creative programming (TOPPHP. ORG) All Rights Reserved
* @ License http://www.opensource.org/licenses/mit-license.php MIT LICENSE
* @ Version 1.0.0-Build20130607
*/
Class Queue {
/**
* Queue.
*
* @ Var array
*/
Private $ queue;
/**
* Queue length.
*
* @ Var integer
*/
Private $ size;
/**
* Constructor-Initialize data.
*/
Public function _ construct (){
$ This-> queue = array ();
$ This-> size = 0;
}
/**
* Queue operations.
*
* @ Param mixed $ data queue data.
* @ Return object returns the object itself.
*/
Public function enqueue ($ data ){
$ This-> queue [$ this-> size ++] = $ data;
Return $ this;
}
/**
* Team-out operations.
*
* @ Return mixed: FALSE is returned when the queue is empty. Otherwise, the Header element of the queue is returned.
*/
Public function dequeue (){
If (! $ This-> isEmpty ()){
-- $ This-> size;
$ Front = array_splice ($ this-> queue, 0, 1 );
Return $ front [0];
}
Return FALSE;
}
/**
* Get the queue.
*
* @ Return array returns the entire queue.
*/
Public function getQueue (){
Return $ this-> queue;
}
/**
* Obtain the element of the team header.
*
* @ Return mixed: FALSE is returned when the queue is empty. Otherwise, the Header element of the queue is returned.
*/
Public function getFront (){
If (! $ This-> isEmpty ()){
Return $ this-> queue [0];
}
Return FALSE;
}
/**
* Get the queue length.
*
* @ Return integer returns the length of the queue.
*/
Public function getSize (){
Return $ this-> size;
}
/**
* Check whether the queue is empty.
*
* @ Return boolean if the queue is empty, TRUE is returned; otherwise, FALSE is returned.
*/
Public function isEmpty (){
Return 0 ===$ this-> size;
}
}
?>
Sample Code 1
<? Php
$ Queue = new Queue ();
$ Queue-> enqueue (1)-> enqueue (2)-> enqueue (3)-> enqueue (4)-> enqueue (5)-> enqueue (6 );
Echo '<pre>', print_r ($ queue-> getQueue (), TRUE), '</pre> ';
$ Queue-> dequeue ();
Echo '<pre>', print_r ($ queue-> getQueue (), TRUE), '</pre> ';
?>
Note: PHP array functions such as queue functions exist: array_unshift (queuing) and array_shift (queuing ).