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. The queue is based on the first-in-first-out or later... "/> <scripttype =" text/javascript "src =" http: // www. 2CT
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
/**
* 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
$ Queue = new Queue ();
$ Queue-> enqueue (1)-> enqueue (2)-> enqueue (3)-> enqueue (4)-> enqueue (5)-> enqueue (6 );
Echo'
', print_r($queue->getQueue(), TRUE), '
';
$ Queue-> dequeue ();
Echo'
', print_r($queue->getQueue(), TRUE), '
';
?>
Note: PHP array functions such as queue functions exist: array_unshift (queuing) and array_shift (queuing ).