Queue, which is a special advanced first line-out table, which can only be deleted at the front end (usually called out of the team), insert operations on the back end (commonly called enqueue). The end of the delete operation is called the team header, and the end of the insert operation is called the tail. Queues are organized according to the principles of FIFO or LIFO. When there are no elements in the queue, it is called an empty queue.
Data structure and algorithm (PHP implementation)-Queue 1
/**
* Data structure and algorithm (PHP implementation)-queue.
*
* @author Gen Want programming (topphp.org)
* @copyright Copyright (c) 2013 Gen want 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;
/**
* The length of the queue.
*
* @var Integer
*/
Private $size;
/**
* Construction Method-initializes the data.
*/
Public Function __construct () {
$this->queue = Array ();
$this->size = 0;
}
/**
* Queue operation.
*
* @param mixed $data the queue data.
* @return Object returns the objects themselves.
*/
Public Function Enqueue ($data) {
$this->queue[$this->size++] = $data;
return $this;
}
/**
* Out of team operation.
*
* Returns false @return mixed empty queue, otherwise returns the team header element.
*/
Public Function dequeue () {
if (! $this->isempty ()) {
--$this->size;
$front = Array_splice ($this->queue, 0, 1);
return $front [0];
}
return FALSE;
}
/**
* Gets the queue.
*
* @return Array returns the entire queue.
*/
Public Function Getqueue () {
return $this->queue;
}
/**
* Get team head elements.
*
* Returns false @return mixed empty queue, otherwise returns the team header element.
*/
Public Function Getfront () {
if (! $this->isempty ()) {
return $this->queue[0];
}
return FALSE;
}
/**
* Gets the length of the queue.
*
* @return Integer Returns the length of the queue.
*/
Public Function GetSize () {
return $this->size;
}
/**
* Detects if the queue is empty.
*
* @return Boolean empty queue returns True, otherwise false is returned.
*/
Public Function IsEmpty () {
return 0 = = = $this->size;
}
}
?>
Example 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), '
';
?>
Description: The PHP array function already has function functions similar to queues: Array_unshift (queued) and, Array_shift (out).
http://www.bkjia.com/PHPjc/477263.html www.bkjia.com true http://www.bkjia.com/PHPjc/477263.html techarticle queue, which is a special advanced first line-out table, which can only be deleted at the front end (usually called out of the team), insert operations on the back end (commonly called enqueue). Into ...