On the queue of PHP data structure

Source: Internet
Author: User

Queue

A queue is a special linear table, except that it only agrees to delete operations at the front end of the table (front), while inserting operations on the back end (rear) of the table, like a stack, which is a linear table of operations constrained. The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header.

The data element of a queue is also known as a queue element. Inserting a queue element in a queue is called queued. Removes a queue element from the queue to become a team. Because the queue only agrees to insert in a paragraph. At the end of the deletion, so that only the first element to enter the queue is first removed from the queue, so the queue is also known as the FIFO (Fifo-first in first out) linear table.


In PHP, queues are often used to make message queues and task queues, and are a useful mechanism. younger brother Beginner. Here is a simple introduction to the basic structure and operation of the queue.


1. Search Queue

The sequential queues in PHP are easy to implement in a few simple lines.

<?

Php

Class queue{

Private $Que =array ();

Team

Public Function Enqueue ($data) {

Return Array_push ($this->que, $data);

}

Out Team

Public Function Delque () {

Return Array_shift ($this->que);

}

}

>

This makes it easy to implement a sequential queue of PHP version numbers.


2. Chain-Queue

Chained queues are often used in queues, where I simply write a link-queue construction method and basic operations:

<?php
Define a member of a chained queue
Class qnode{
public $data;//Member Data
public $next;//point to next member
public $id;//Member ID
Public function __construct ($data =null, $next =null, $id =0) {
$this->data= $data;
$this->next= $next;
$this->id= $id;
}
}
defining queues
Class linkqueue{
private $front;//Team Head
private $rear;//Team Tail
Public Function __construct () {
$this->front=new Qnode ($data =null, $next =null, $id =0);
$this->rear= $this->front;
}
Define Queue
Public Function EnQueue ($data) {
$front = $this->front;
$q = new Qnode ($data, NULL, $this->rear->id+1);
$this->rear->next= $q;
$this->rear= $q;
}
Out Team
Public Function Delqueue () {
$temp = $this->front->next->data;
$this->front= $this->front->next;
$this->front->data=null;
return $temp;
}
Get Queue Length
Public Function GetLength () {
Return ($this->rear->id-$this->front->id);
}
Get queue
Public Function Getlinkqueue () {
$front = $this->front;
while ($front! = $this->rear) {
$front = $front->next;
Echo ' $front->id. ' Members for '. $front->data. ' <br> ';
}
}
}

$a =new linkqueue;
$a->enqueue (' a ');
$a->enqueue (' B ');
$a->enqueue (' C ');
$a->enqueue (' d ');
$a->enqueue (3);
Echo ' Out of the team has always been co-owned '. $a->getlength (). ' Players <br> ';
echo $a->delqueue (). ' Run out of the team after <br> ';
echo $a->getlinkqueue ();
Echo ' altogether remaining '. $a->getlength (). ' A member ';
?>

The last few behavioral test code. Here's the result:

Hope to help you understand the structure of the list, the younger brother beginners, if there is not enough to write a thorough, I hope you include more.

On the queue of PHP data 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.