<?phpclass QNode{ public $data; public $next public function __construct ($data) { $this->data= $data; $this->next= The null; }}class linkqueue{ //chain queue contains the head node, which is empty when instantiated private $data; private $next; private $front;//point to head node private $rear;//pointing to the tail knot// private $length; public function __construct () { $this data=null; $this->next=null; $this->front= $this, //point $this- >rear= $this;//point to head node// $this->length=0; } //Destroy Queue public function destroyqueue () { while ($this->front) { //destruction begins with the beginning of the first node $this->rear= $this->front->next; unset ($this->front); $this->front= $this->rear; } } //empty Queue public function clearqueue () { $p = $this->front->next; while ($p) { $q = $p next; unset ($p); $p = $q; } $this->front->next=null; $this->rear=$ this->front; } //whether the queue is empty public Function queueempty () { if ($this->front== $this->rear) { return ' Null '; }else{ return ' No null '; } } //Queue Length public function queuelength () { $p = $this->front; $i =0; while ($p != $this->rear) { $i ++; $p = $p->next; } return $i;// return $this->length; } //get team head element public function gethead () { if ($this->front== $this->rear) { return ' ERROR '; } return $this->front->next->data; } Insert Element public function enqueue () { from end of team $node =new qnode (Mt_rand (100,200)); $node next= $this->rear->next; $this->rear->next= $node; $this->rear= $node; $this->length++; } //remove elements from Team head public function dequeue () { if ($this->front== $this->rear) { return ' ERROR '; } $p = $this Front->next; unset ($this->front->next); &nBSP; $this->front->next= $p->next; if ($this->rear== $p { //If there is only one element, then the pointer needs to change. $this->rear= $this->front; } $this->length--; return ' OK '; } //Traversal queue Element public function queuetraverse () { if ($this->front== $this->rear) { return ' ERROR '; } $arr =array (); $p = $this->front- >next; while ($p) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; $arr []= $p->data; $p = $p->next; } return $arr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1600889
Queue of data structure--chained storage structure (PHP code implementation)