Step 1: In the list, lower priority number is 1, whose data value is 333, so it will be inserted in the list as shown in the below diagram: In the case of priority queue, lower priority number is considered the higher priority, i.e., lower priority number = higher priority. Let's create the priority queue step by step. We will create the priority queue by using the list given below in which INFO list contains the data elements, PRN list contains the priority numbers of each data element available in the INFO list, and LINK basically contains the address of the next node. Now, we will see how to represent the priority queue through a one-way list. For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1 therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue. Descending order priority queue: In descending order priority queue, a higher priority number is given as a higher priority in a priority.For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5 therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue. Ascending order priority queue: In ascending order priority queue, a lower priority number is given as a higher priority in a priority.add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the third highest priority in a priority queue. ![]() poll(): It will remove '2' element from the priority queue as it has the highest priority queue.As 2 is the smallest element among all the numbers so it will obtain the highest priority. add(2): This function will insert '2' element in a priority queue.In the above priority queue, the '1' element has the highest priority, so it will be removed from the priority queue. poll(): This function will remove the highest priority element from the priority queue.Now, we will observe how the priority queue will look after performing the following operations: We have a priority queue that contains the following values:Īll the values are arranged in ascending order. Let's understand the priority queue through an example. If two elements in a priority queue have the same priority, they will be arranged using the FIFO principle.An element with the higher priority will be deleted before the deletion of the lesser priority.Every element in a priority queue has some priority associated with it.Characteristics of a Priority queueĪ priority queue is an extension of a queue that contains the following characteristics: Therefore, the 1 number would be having the highest priority while 22 will be having the lowest priority. The priority queue supports only comparable elements, which means that the elements are either arranged in an ascending or descending order.įor example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering imposed on the values is from least to the greatest. The priority of the elements in a priority queue will determine the order in which elements are removed from the priority queue. Print(, "\t", priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some priority, i.e., the element with the highest priority would come first in a priority queue. Adding the Node to the Priority Queue based on its priority value, and in case of similar priority, according to the First in First out (FIFO) precedence if inst.priority = :Įlif inst.priority = :. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |