php - PHP 中的优先级队列

标签 php queue priority-queue

我正在尝试使用此代码创建优先级队列,但找不到问题所在。有人告诉我哪里错了。

<?php

class PriorityQueue implements Iterator , Countable
{
  public function __construct() {
    $flags = self::EXTR_DATA;
    $items = array();
  }

  function compare ( mixed $priority1 , mixed $priority2 ){}
  function count (){
    return count($this->items);
  }

  function current (){
    switch ($this->flags) {
    case self::EXTR_BOTH:
      $ret = array();
      $ret['Patient'] = current($this->items);
      $ret['Priority'] = $this->key();
      break;
    case self::EXTR_DATA:
      $ret = current($this->items);
      break;
    case self::EXTR_PRIORITY:
      $ret = $this->key();
      break;
    };
    return $ret;
  }

  function extract (){
    $ret = $this->current();
    $this->next();
    return $ret;
  }

  function insert ($name,$priority){
    $patient = array();

    return $patient[$name] = $priority;
  }

  function isEmpty ()
  {
    return empty($this->items);
  }

  function  key (){
    return substr(key($this->items), 0, 9);
  }

  function next (){
    //array_shift($this->items);
    return($this->items);
    echo "<br />";
  }
  function recoverFromCorruption (){}
  function rewind (){}

  function setExtractFlags (int $flags ){
    switch ($flags) {
    case self::EXTR_BOTH:
    case self::EXTR_DATA:
    case self::EXTR_PRIORITY:
      $this->flags = $flags;
      break;
    };
  }

  function  top (){
    return $this->current();
  }

  function valid () {
    if (NULL !== key($this->items)) {
      return TRUE;
    }
    return FALSE;
  }// function valid
  /**
   * Extract the data.
   */
  const EXTR_DATA = 1;
  /**
   * Extract the priority.
   */
  const EXTR_PRIORITY = 2;
  /**
   * Extract an array containing both priority and data.
   */
  const EXTR_BOTH = 3;
};


$objPQ = new splPriorityqueue();
$objPQ->insert('Richard',9);
$objPQ->insert('paul',1);
$objPQ->insert('Ken',8);
$objPQ->insert('peter',2);
$objPQ->insert('Rick',7);
$objPQ->insert('Dan',5);



echo "PATIENTS = ".$objPQ->count()."<br />";

//mode of extraction
$objPQ->setExtractFlags(splPriorityqueue::EXTR_BOTH);

//Go to TOP
$objPQ->top();

for($i=0,$j=$objPQ->count(); $i<$j; $i++){
  //print_r($objPQ->current());

  $patients = $objPQ->current();
  foreach ($patients as $patient=>$value){
    echo $patient."<br />".$value;

    $objPQ->next();
    echo "<br />";
  }
}

?>

我现在得到一些奇怪的结果

data-patient Richard
priority-9
......
etc

我想要得到的结果是

Richard - 9
Ken - 8
Rick - 7
Dan - 5
Peter - 2
Paul - 1

考虑优先级

最佳答案

标准 PHP 库 (SPL) 实现 SplPriorityQueue类:

$pq = new SplPriorityQueue();

// The insert method inserts an element in the queue by shifting it up
$pq->insert('A', 3);
$pq->insert('B', 6);
$pq->insert('C', 1);
$pq->insert('D', 2);

// Count the elements
echo "count ->" . $pq->count() . PHP_EOL;

// Sets the mode of extraction (EXTR_DATA, EXTR_PRIORITY, EXTR_BOTH)
$pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);

// Go at the node from the top of the queue
$pq->top();

// Iterate the queue (by priority) and display each element
while ($pq->valid()) {
    print_r($pq->current());
    echo PHP_EOL;
    $pq->next();
}

关于php - PHP 中的优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386107/

相关文章:

Java 优先级队列中的并发修改异常

PHP文件上传在本地工作但不在服务器上工作

javascript - 如何访问ajax调用返回的对象中的数据

java - 比较方法违反了它的一般契约!优先级队列错误

amazon-web-services - RabbitMQ可见性超时

java - 为什么 async-http-client 不限制我的请求?

Java PriorityQueue 和 Comparable 接口(interface)

php - youtube-api 3.0 更新视频描述和/或标题

php - 将两个字段插入数据库

java - 如何使用for循环将不同的对象添加到队列中