This article introduces the implementation of a queue in PHP data structure of a code, is a good example of learning queue operation, a friend need to refer to the next bar. What is a queue? 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. Below is a shared, PHP implementation of the data structure with the algorithm-queuing (queue) code. As follows:
Queue = Array (); $this->size = 0; }/** * Queued operation. * * @param mixed $data queue data. * @return Object returns the objects themselves. */Public Function Enqueue ($data) {$this->queue[$this->size++] = $data; return $this; }/** * Out of the 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; }/** * Gets the team head element. * * 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; }}?> Invocation Example:
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). |