Implementation code for the PHP queue data structure

Source: Internet
Author: User
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).

  • Related Article

    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.