PHP queue

WBOY
Release: 2024-08-29 13:01:13
Original
1217 people have browsed it

Queue in PHP is a data structure that operates based on First In First Out which is also called FIFO, and four basic operations define a queue, namely init, enqueue, dequeue and isEmpty, where init operation is used for the creation of the queue and enqueue operation is used to add an item at the end of the queue, or the tail of the queue and dequeue operation is used to remove an item from the front of the queue or the head of the queue and isEmpty operation is used to check if the queue is empty or not that is it returns if the queue contains no more items or not.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax to declare queue in PHP is as follows:

enqueue(item_to_added_to_the_queue);
dequeue();
Copy after login

where item_to_be_added_to_the_queue is the item that is going to be added to the queue at the end of the queue or the tail of the queue.

Working of the queue in PHP

  • Queue in PHP is a data structure that operates based on First In First Out, which is also called FIFO.
  • Four basic operations define a queue, namely init, enqueue, dequeue, and isEmpty.
  • init operation is used for the creation of the queue.
  • enqueue operation is used to add an item at the end of the queue or the tail of the queue.
  • The dequeue operation is used to remove an item from the front of the queue or the head of the queue.
  • isEmpty operation is used to check if the queue is empty or not; that is, it returns if the queue contains no more items or not.

Examples of PHP queue

Here are the following examples mention below

Example #1

PHP program to add the items to a queue from the end of the queue using enqueue() function and remove the items from the front of the queue using the dequeue() function, and display the contents of the queue:

Code:

<?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);
?>
Copy after login

Output:

PHP queue

In the above program, we are creating an instance of the SplQueue() class. Then we are adding items to the queue from the tail of the queue or the end of the queue. Then we are making use of the rewind() function to bring the file pointer to the beginning of the queue. Then we are using the valid() function to check if the queue is valid or not after using the rewind() function and then displaying the elements of the queue. Then we are printing the contents of the queue in a human-readable format by using the print_r function. Then we remove the first two items from the head of the queue using the dequeue() function and then display the queue contents after using the dequeuer() function in human-readable form using print_r function. The output is shown in the snapshot above.

Example #2

PHP program to add the items to a queue from the end of the queue using enqueue() function and remove the items from the front of the queue using the dequeue() function, and display the contents of the queue:

Code:

<?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);
?>
Copy after login

Output:

PHP queue

In the above program, we are creating an instance of the SplQueue() class. Then we are adding items to the queue from the tail of the queue or the end of the queue. Then we are making use of the rewind() function to bring the file pointer to the beginning of the queue.

Then we are using the valid() function to check if the queue is valid or not after using the rewind() function and then displaying the elements of the queue. Then we are printing the contents of the queue in a human-readable format by using the print_r function. Then we remove all the three items from the head of the queue using the dequeue() function and then display the queue contents after using the dequeuer() function in human-readable form using print_r function, which is an empty queue. The output is shown in the snapshot above.

The above is the detailed content of PHP queue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!