Exemple #1
0
 public static function testStructure()
 {
     $queue = new Queue();
     if ($queue->getCount() !== 0) {
         self::printError("problem creating a new queue - count not 0");
     }
     // test enqueue
     $queue->enqueue(1);
     $queue->enqueue(2);
     $queue->enqueue(3);
     $queue->enqueue(4);
     $queue->enqueue(5);
     if ($queue->getCount() !== 5) {
         self::printError("problem enqueueing.  count not 5");
     }
     // display... for now
     $queue->display();
     // test dequeue
     $data = $queue->dequeue();
     if ($data !== 1) {
         self::printError("problem dequeueing. value not 1");
     }
     if ($queue->getCount() !== 4) {
         self::printError("problem dequeueing. count not 4");
     }
     $data = $queue->dequeue();
     $data = $queue->dequeue();
     $data = $queue->dequeue();
     $data = $queue->dequeue();
     if ($data !== 5) {
         self::printError("problem dequeueing. value not 5");
     }
     if ($queue->getCount() !== 0) {
         self::printError("problem dequeueing. count not 0");
     }
     // display... for now
     $queue->display();
 }
Exemple #2
0
        return $this->first == null;
    }
    public function enqueue($item)
    {
        $oldlast = $this->last;
        $this->last = new Node();
        $this->last->item = $item;
        $this->last->next = null;
        if ($this->isEmpty()) {
            $this->first = $this->last;
        } else {
            $oldlast->next = $this->last;
        }
    }
    public function dequeue()
    {
        $item = $this->first->item;
        $this->first = $this->first->next;
        if ($this->isEmpty()) {
            $this->last = null;
        }
        return $item;
    }
}
$list = new Queue();
$list->enqueue(1);
$list->enqueue(2);
$list->enqueue(3);
echo $list->dequeue();
echo $list->dequeue();
echo $list->dequeue();
     */
    public function enqueue($el)
    {
        array_push($this->_queue, $el);
    }
    /**
     * Dequeue the front element from the queue.
     *
     * @return mixed $element.
     */
    public function dequeue()
    {
        if ($this->front() !== null) {
            return array_shift($this->_queue);
        }
        return null;
    }
}
// Example usage:
$queue = new Queue();
$queue->enqueue("Element");
$queue->enqueue(23);
$queue->enqueue(array(3, 4, 6));
while (!$queue->is_empty()) {
    echo "The front element is now : \n";
    var_dump($queue->front());
    echo "\n";
    $queue->dequeue();
}
// Outputs NULL
var_dump($queue->front());
Exemple #4
0
    protected $dataStore = [];
    public function enqueue($element)
    {
        array_push($this->dataStore, $element);
        parent::enqueue($element);
    }
    public function dequeue()
    {
        array_shift($this->dataStore);
        return parent::dequeue();
    }
    public function toString()
    {
        $retStr = "";
        $cnt = count($this->dataStore);
        for ($i = 0; $i < $cnt; ++$i) {
            $retStr .= $this->dataStore[$i] . "\n";
        }
        return $retStr . "\n";
    }
}
$users = ["Вася", "Петя", "Федя", "Саша", "Зина", "Маша"];
$q = new Queue();
foreach ($users as $user) {
    $q->enqueue($user);
}
echo "Кто в очереди:\n" . $q->toString();
echo $q->dequeue() . " вышел\n";
echo "Кто в очереди:\n" . $q->toString();
echo "Кто первый: " . $q->bottom() . "\n";
echo "Кто последний: " . $q->top() . "\n";
Exemple #5
0
            return false;
        }
    }
    public function isFull()
    {
    }
}
$q = new Queue(5);
// instantiates the Queue class with a maxSize attribute of 5
$q->isEmpty();
// returns true
$q->enqueue(100);
// Queue: 100
$q->rear();
// returns 100
$q->front();
// returns 100
$q->enqueue(20);
// Queue: 100, 20
$q->enqueue(2);
// Queue: 100, 20, 2
$q->dequeue();
// Queue: 20, 2
$q->enqueue(500);
// Queue: 20, 2, 500
$q->enqueue(12);
// Queue: 20, 2, 500, 12
$q->enqueue(30);
// Queue: 20, 2, 500, 12, 30
$q->isFull();
// returns true
Exemple #6
0
 /**
  * Forms the clusters by removing maximum weighted edges.
  * performs breadth-first search to cluster the recipes.
  *
  * @param int $k queue size
  * @param int $size number of recipes.
  * @return array $cluster clusters of recipes.
  */
 function formCluster($k, $size)
 {
     $this->cluster_heap->top();
     $nodeQueue = new Queue($k);
     $cluster_count = $size * CLUSTER_RATIO;
     $cluster = array();
     /*
        Idea remove $cluster_count many weightiest edges from tree
        to get a forest. As do this add to queue end points of
        removed edges.
     */
     for ($j = 0; $j < $cluster_count - 1; $j++) {
         $max_edge = $this->cluster_heap->extract();
         $cluster1_start = $max_edge->getStartVertex()->getLabel();
         $cluster2_start = $max_edge->getEndVertex()->getLabel();
         $this->adjMatrix[$cluster1_start][$cluster2_start] = -1;
         $this->adjMatrix[$cluster2_start][$cluster1_start] = -1;
         $nodeQueue->enqueue($cluster1_start);
         $nodeQueue->enqueue($cluster2_start);
     }
     $queue = new Queue($k);
     $i = 0;
     // Now use Queue above to make clusters (trees in resulting forest)
     while (!$nodeQueue->isEmpty()) {
         $node = $nodeQueue->dequeue();
         if ($this->vertices[$node]->isVisited() == false) {
             $this->vertices[$node]->visited();
             $cluster[$i][] = $this->vertices[$node]->getLabel();
             $queue->enqueue($this->vertices[$node]->getLabel());
             while (!$queue->isEmpty()) {
                 $node = $queue->dequeue();
                 while (($nextnode = $this->getNextVertex($node)) != -1) {
                     $this->vertices[$nextnode]->visited();
                     $cluster[$i][] = $this->vertices[$nextnode]->getLabel();
                     $queue->enqueue($this->vertices[$nextnode]->getLabel());
                 }
             }
         }
         $i++;
     }
     return $cluster;
 }