2013-05-05 185 views
0

我想用這段代碼創建一個優先級隊列,但我找不到問題所在。有人告訴我我錯在哪裏。PHP中的優先級隊列

<?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 

考慮給予

+0

你縮進代碼,因此它更容易閱讀? (這對你和讀者都有好處!)。另外,你目前得到的結果是什麼?請將其編輯到問題中,並解釋其原因。 – halfer 2013-05-05 15:34:57

+1

推測'splPriorityqueue'和'PriorityQueue'意思是一樣的東西? – halfer 2013-05-05 15:36:52

+0

我在哪裏可以得到** PriorityQueue **類? – Richie 2013-05-05 15:46:21

回答

0

試試這個修改後的類優先級:

class PriorityQueue implements Iterator, Countable { 
    /** 
    * 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; 
    private $flags; 
    private $items; 

    public function __construct() { 
     $this->flags = self::EXTR_DATA; 
     $this->items = array(); 
    } 

    function compare($priority1, $priority2) {} 

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

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

    function current() { 
     switch ($this->flags) { 
      case self::EXTR_BOTH: 
       $result = $this->key() . ' - ' . current($this->items); 
       break; 
      case self::EXTR_DATA: 
       $result = $this->key(); 
       break; 
      case self::EXTR_PRIORITY: 
       $result = current($this->items); 
       break; 
      default: 
       $result = ''; 
     } 
     return $result; 
    } 

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

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

    function insert($name, $priority) { 
     $this->items[$name] = $priority; 
     asort($this->items); 
     return $this; 
    } 

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

    function recoverFromCorruption() {} 

    function rewind() {} 

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

    function valid() { 
     return (null === key($this->items)) ? false : true; 
    } 
} 

用法:

$patients = new PriorityQueue(); 
$patients->setExtractFlags(PriorityQueue::EXTR_BOTH); 

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

foreach($patients as $patient) { 
    echo $patient->current(); 
} 
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(); 
} 
+1

謝謝!那麼,這是我的大學生項目,我已經完成了。已經畢業並獲得工作!不管怎麼說,還是要謝謝你! :-) – Richie 2014-08-11 03:34:27