PHP Implementation Loop Queue (sequential structure)

Source: Internet
Author: User

Queue: FIFO, is a widely used data structure, whether it is storage or message, or to perform tasks, and so on, life and software used in the chestnut abound.


Not much to say directly on the code, refer to many, think this is the most in line with the central idea. In order to distinguish whether the queue is full, we still decide to use the empty one is full, as follows

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9C/76/wKioL1lwlnCwmTTVAAJBGlEjR8c807.png "title=" Queue full judgment. png "alt=" Wkiol1lwlncwmttvaajbglejr8c807.png "/>



Directly on the code

Cyclic queue, Sequential storage class Queue{private $data;p rivate $front;p rivate $rear;//define maximum capacity const max=5; Define quantity//Initialize the queue, both the head and tail pointers point to only 0, and our full rule is empty. function __construct () {$this->data=array (); $this->front=0; $this->rear=0;echo "queue ready OK";} An interesting little formula because we stipulate that the middle interval one is full, the principle and the picture public Function queuelength () {echo ($this->rear-$this->front+self::max)% Self::max;} Enter queue//Do not need to add this queue parameter, with $thispublic function InQueue ($e) {if (($this->rear+1)% (self::max) = = $this->front) {echo ' The queue is full ';d ie;} $this->data[$this->rear]= $e $this->rear= ($this->rear+1)% (Self::max); Echo ' OK ';} Public Function DeQueue () {if ($this->front== $this->rear) {echo ' queue is empty ';d ie;} var $tmp;//$tmp = $this->data[$this->front]= "; $this->front = ($this->front+1)%self::max;//unset ($ this->data[$this->front]); Echo ' OK ';}}

The test code is as follows:

$obj =new Queue (), $obj->inqueue (1), $obj->inqueue (2), Var_dump ($obj), $obj->dequeue (), Var_dump ($obj); $obj- >inqueue (3); Var_dump ($obj), $obj->inqueue (4), Var_dump ($obj), $obj->inqueue (5), Var_dump ($obj), $obj DeQueue (); Var_dump ($obj); $obj->dequeue (); Var_dump ($obj); $obj->dequeue (); Var_dump ($obj); $obj->dequeue ( ); Var_dump ($obj); $obj->queuelength (); $obj->inqueue (1); Var_dump ($obj); $obj->inqueue (2); Var_dump ($obj);

There is a little poem I think the author is very good, add a little literary flavor

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9C/76/wKiom1lwl1CjN9PdAAIuKwQDA88460.png "title=" Life and the queue--good, Great little poem. 1.png "alt=" Wkiom1lwl1cjn9pdaaiukwqda88460.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9C/76/wKioL1lwl1HBICNBAAJMyh01H_Y993.png "title=" Life and the queue--good, Great little poem. 2.png "alt=" Wkiol1lwl1hbicnbaajmyh01h_y993.png "/>



Thank you, may all sentient beings in the law world be well

This article is from the "one-stop solution" blog, so be sure to keep this source http://10725691.blog.51cto.com/10715691/1949508

PHP Implementation Loop Queue (sequential structure)

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.