Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds\PriorityQueue::push() Function



The PHP Ds\PriorityQueue::push() function is used to push the values with a given priority into a queue. This function does not return value but it modifies the priority queue by adding e new element.

Syntax

Below is the syntax of the PHP Ds\PriorityQueue::push() function −

public void Ds\PriorityQueue::push( mixed $value , int $priority )

Parameters

Below are the parameters of the push() function −

  • $value − It is the value to be added into the queue.

  • $priority − It is the priority associated with the value. It can be an integer or a float.

Return Value

The push() function does not return any value. The priority queue is updated with the addition of the new element.

PHP Version

The push() function is available from version 1.0.0 of the Ds extension onwards.

Example 1

First we will show you the basic example of the PHP Ds\PriorityQueue::push() function to add elements in the priority queue.

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();  
   $pqueue->push("Tutorials", 1); 
   $pqueue->push("Point", 2); 
   $pqueue->push("India", 3); 
  
   echo "The PriorityQueue is: \n"; 
   print_r($pqueue);
?>

Output

The above code will result something like this −

The PriorityQueue is: 
Ds\PriorityQueue Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)

Example 2

Here we will use the push() function to add elements with different priorities for to do tasks.

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();
   
   // Push elements with different priorities
   $pqueue->push("Clean the house", 1);
   $pqueue->push("Finish homework", 5);
   $pqueue->push("Go shopping", 3);
   
   // Print the queue
   foreach ($pqueue as $task) {
       echo $task . "\n";
   }
?> 

Output

This will generate the below output −

Finish homework
Go shopping
Clean the house

Example 3

Now we will use float values for different priorities and use push() function to add new elements in the PriorityQueue.

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();
   
   // Push elements with float priorities
   $pqueue->push("Read a book", 2.5);
   $pqueue->push("Play games", 1.2);
   $pqueue->push("Exercise", 3.7);
   
   // Print the queue
   foreach ($pqueue as $activity) {
       echo $activity . "\n";
   }
?> 

Output

This will create the below output −

Exercise
Read a book
Play games

Example 4

In the following example, we are using the push() function to to add new items in the priority queue and handle same priority.

<?php
   // Create a new PriorityQueue
   $pqueue = new \Ds\PriorityQueue();
   
   // Push elements with the same priority
   $pqueue->push("Task A", 2);
   $pqueue->push("Task B", 2);
   $pqueue->push("Task C", 2);
   
   // Print the queue
   foreach ($pqueue as $task) {
       echo $task . "\n";
   }
?> 

Output

Following is the output of the above code −

Task A
Task B
Task C
php_function_reference.htm
Advertisements