You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

To implement Queue, the easiest way can do it is using array() to store elements, and array_shift() to extract elements at the front of the Queue.

Below is a simple class for Queue.

<?php

class TQueue {

 protected $_storage = array();

 public function add( $item ) {
  $this->_storage[] = $item;
 }

 function get() {
  return array_shift($this->_storage);
 }

};


Below is a simple example to add text strings, and get until empty:

<?php

class TQueue {

 protected $_storage = array();

 public function add( $item ) {
  $this->_storage[] = $item;
 }

 function get() {
  return array_shift($this->_storage);
 }

};

$q = new TQueue;

$q->add( "hello");
$q->add( "world");

while(1)
{
        $v = $q->get();
        if (!$v) break;
        echo $v . "\n";
}

?>


  • No labels