PHP implements the Queue Data Structure

Source: Internet
Author: User

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 ).

 

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.