×


Implementing priority queue in PHP

Queue is a First In First Out(FIFO) Data Structure.In a priority queue, an element with high priority is served before an element with low priority.The Standard PHP Library has the SplPriorityQueue class which can be used to implement priority queue.The insert(‘item’,priority) method is used to insert item and the corresponding priority in to the queue.See the below example to see mail sending to different mail id’s that have different priorities.You can use while to iterate thorough the queue and process each one or the results.

SplPriorityQueue

A priority queue can be implemented using many of the data structures that we’ve already studied (array, inked list, binary search tree). However, those data structures do not provide the most efficient operations. To make all of the operations  efficient, heap can be used . SplPriorityQueue is implemented using Max Heap

 

 

 

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *