PHP - Ds\PriorityQueue::isEmpty() 函式



PHP 的 **Ds\PriorityQueue::isEmpty()** 函式用於檢查佇列是否為空。此函式不接受任何引數,如果佇列為空則返回 true,如果佇列不為空則返回 false。

語法

以下是 PHP **Ds\PriorityQueue::isEmpty()** 函式的語法:

public bool Ds\PriorityQueue::isEmpty( void )

引數

此函式沒有任何引數。

返回值

如果佇列為空,**isEmpty()** 函式返回 true;失敗則返回 FALSE。

PHP 版本

此函式從 Ds 擴充套件的 1.0.0 版本開始可用。

示例 1

在這裡,我們將向您展示 PHP **Ds\PriorityQueue::isEmpty()** 函式的基本示例,以檢查佇列是否為空並相應地列印訊息。

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();

   // Check if the queue is empty
   echo $pqueue->isEmpty() ? 'The queue is empty' : 'The queue is not empty'; 
?> 

輸出

以下是上述程式碼的輸出:

The queue is empty

示例 2

在下面的 PHP 程式碼中,我們將嘗試在建立的佇列中插入元素後使用 **isEmpty()** 函式,並檢查佇列是否為空,然後列印訊息。

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();
   $pqueue->push('A', 1);
   $pqueue->push('B', 2);
   
   echo $pqueue->isEmpty() ? 'The queue is empty' : 'The queue is not empty'; 
?> 

輸出

這將生成以下輸出:

The queue is not empty

示例 3

現在,在下面的程式碼中,我們將向佇列中新增新元素並彈出每個元素,然後使用 **isEmpty()** 函式檢查佇列是否為空。

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();
   $pqueue->push('A', 1);
   $pqueue->push('B', 2);
   
   $pqueue->pop();
   $pqueue->pop();
   
   echo $pqueue->isEmpty() ? 'The queue is empty' : 'The queue is not empty';
?> 

輸出

這將建立以下輸出:

The queue is empty

示例 4

在下面的示例中,我們使用 **isEmpty()** 函式檢查兩個佇列,一個為空,另一個不為空。

<?php
   // Create a new PriorityQueue   
   $pqueue = new \Ds\PriorityQueue();  
         
   var_dump($pqueue->isEmpty()); 
        
   $pqueue->push("Tutorials", 1); 
   $pqueue->push("Point", 2); 
   $pqueue->push("India", 3); 
        
   var_dump($pqueue->isEmpty()); 
?>

輸出

上述程式碼將產生類似以下的結果:

bool(true)
bool(false)
php_function_reference.htm
廣告