Priority queue4/4/2023 Step 3: After inserting the elements of priority 2, the next higher priority number is 4 and data elements associated with 4 priority numbers are 444, 555, 777. So, this data will be inserted based on the FIFO principle therefore 222 will be added first and then 111. Step 2: After inserting 333, priority number 2 is having a higher priority, and data values associated with this priority are 222 and 111. 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.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |