首頁  >  文章  >  後端開發  >  PHP佇列

PHP佇列

WBOY
WBOY原創
2024-08-29 13:01:13965瀏覽

PHP中的佇列是一種基於先進先出(First In First Out)操作的資料結構,也稱為先進先出(FIFO),定義佇列的四個基本操作,分別是init、enqueue、dequeue和isEmpty,其中init 操作用於創建隊列隊列和入隊操作用於在隊列尾部添加項目,或使用隊列尾部和出隊操作用於從隊列前端或隊列頭部刪除項目,並使用isEmpty 操作檢查隊列是否為空,如果佇列不包含更多項目,則傳回。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP 中宣告佇列的語法如下:

enqueue(item_to_added_to_the_queue);
dequeue();

其中 item_to_be_added_to_the_queue 是要加入到佇列末端或佇列尾部的項目。

PHP 中隊列的工作

  • PHP中的佇列是一種基於先進先出的資料結構,也稱為先進先出。
  • 定義佇列的四個基本操作,即 init、enqueue、dequeue 和 isEmpty。
  • init操作用於佇列的建立。
  • 入隊操作用於在佇列末尾或佇列尾部新增一個項目。
  • 出隊操作用於從佇列的前面或佇列的頭部刪除一個項目。
  • isEmpty 操作用於檢查佇列是否為空;也就是說,如果佇列不再包含任何項目,它就會傳回。

PHP 隊列範例

以下是下面提到的範例

範例#1

PHP 程式使用 enqueue() 函數從佇列末尾新增項目到佇列,並使用 dequeue() 函數從佇列前端刪除項目,並顯示佇列的內容:

代碼:

<?php
//creating an instance of SplQueue class
$newqueue = new SplQueue();
//using enqueue() function to the add items to the queue from the tail of the queue
$newqueue->enqueue('Welcome');
$newqueue->enqueue('to');
$newqueue->enqueue('PHP');
//using rewind() function to bring the file pointer to the beginning of the queue
$newqueue->rewind();
//using valid() function to check if the queue is valid or not after using rewind() function and then displaying the elements of the queue
while($newqueue->valid()){
echo $newqueue->current(),"\n";
$newqueue->next();
}
//printing the contents of the queue in a human readable format by using print_r function
print_r ($newqueue);
//Removing the first two items from the head of the queue using dequeue() function and then displaying the contents of the queue in human readable form using print_r function
$newqueue->dequeue();
$newqueue->dequeue();
print_r ($newqueue);
?>

輸出:

PHP佇列

在上面的程式中,我們建立了 SplQueue() 類別的實例。然後我們從佇列尾部或佇列末尾將項目新增到佇列中。然後我們使用 rewind() 函數將檔案指標帶到佇列的開頭。然後,在使用 rewind() 函數之後,我們使用 valid() 函數檢查佇列是否有效,然後顯示佇列的元素。然後,我們使用 print_r 函數以人類可讀的格式列印佇列的內容。然後,我們使用 dequeue() 函數從佇列頭部刪除前兩項,然後使用 print_r 函數以人類可讀的形式使用 dequeuer() 函數後顯示佇列內容。輸出如上面的快照所示。

範例#2

PHP 程式使用 enqueue() 函數從佇列末尾新增項目到佇列,並使用 dequeue() 函數從佇列前端刪除項目,並顯示佇列的內容:

代碼:

<?php
//creating an instance of SplQueue class
$newqueue = new SplQueue();
//using enqueue() function to the add items to the queue from the tail of the queue
$newqueue->enqueue('Welcome');
$newqueue->enqueue('to');
$newqueue->enqueue('EDUCBA');
//using rewind() function to bring the file pointer to the beginning of the queue
$newqueue->rewind();
//using valid() function to check if the queue is valid or not after using rewind() function and then displaying the elements of the queue
while($newqueue->valid()){
echo $newqueue->current(),"\n";
$newqueue->next();
}
//printing the contents of the queue in a human readable format by using print_r function
print_r ($newqueue);
//Removing the first two items from the head of the queue using dequeue() function and then displaying the contents of the queue in human readable form using print_r function
$newqueue->dequeue();
$newqueue->dequeue();
$newqueue->dequeue();
print_r ($newqueue);
?>

輸出:

PHP佇列

在上面的程式中,我們建立了 SplQueue() 類別的實例。然後我們從佇列尾部或佇列末尾向佇列新增項目。然後我們使用 rewind() 函數將檔案指標帶到佇列的開頭。

然後,在使用 rewind() 函數之後,我們使用 valid() 函數檢查佇列是否有效,然後顯示佇列的元素。然後,我們使用 print_r 函數以人類可讀的格式列印佇列的內容。然後我們使用 dequeue() 函數從佇列頭部刪除所有三個項目,然後使用 print_r 函數以人類可讀的形式顯示使用 dequeuer() 函數後的佇列內容,這是一個空隊列。輸出如上面的快照所示。

以上是PHP佇列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP堆疊下一篇:PHP堆疊