PHPキュー

WBOY
リリース: 2024-08-29 13:01:13
オリジナル
961 人が閲覧しました

PHP のキューは、FIFO とも呼ばれる先入れ先出しに基づいて動作するデータ構造であり、4 つの基本操作 (init、enqueue、dequeue、isEmpty) によってキューが定義されます。init 操作はキューの作成に使用されます。キューとエンキュー操作はキューの最後に項目を追加するために使用され、キューの末尾とデキュー操作はキューの先頭またはキューの先頭から項目を削除するために使用され、isEmpty 操作は使用されます。キューが空かどうかを確認します。つまり、キューに項目がもうないかどうかを返します。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP でキューを宣言する構文は次のとおりです:

enqueue(item_to_added_to_the_queue);
dequeue();
ログイン後にコピー

ここで、 item_to_be_added_to_the_queue は、キューの最後またはキューの最後尾でキューに追加されるアイテムです。

PHP でのキューの仕組み

  • PHP のキューは、FIFO とも呼ばれる先入れ先出しに基づいて動作するデータ構造です。
  • 4 つの基本操作 (init、enqueue、dequeue、isEmpty) でキューを定義します。
  • キューの作成には init オペレーションが使用されます。
  • エンキュー操作は、キューの最後またはキューの最後尾に項目を追加するために使用されます。
  • デキュー操作は、キューの先頭またはキューの先頭から項目を削除するために使用されます。
  • isEmpty オペレーションは、キューが空かどうかを確認するために使用されます。つまり、キューに項目がもうないかどうかを返します。

PHP キューの例

以下に挙げる例は次のとおりです

例 #1

enqueue() 関数を使用してキューの最後から項目をキューに追加し、dequeue() 関数を使用してキューの先頭から項目を削除し、キューの内容を表示する PHP プログラム:

コード:

<?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() 関数を使用してキューの先頭から最初の 2 つの項目を削除し、 dequeuer() 関数を使用した後、print_r 関数を使用してキューの内容を人間が判読できる形式で表示します。出力は上のスナップショットに示されています。

例 #2

enqueue() 関数を使用してキューの最後から項目をキューに追加し、dequeue() 関数を使用してキューの先頭から項目を削除し、キューの内容を表示する PHP プログラム:

コード:

<?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() 関数を使用してキューの先頭から 3 つの項目すべてを削除し、 dequeuer() 関数を使用した後、空のキューである print_r 関数を使用してキューの内容を人間が判読できる形式で表示します。出力は上のスナップショットに示されています。

以上がPHPキューの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!