Do you know what is Priority Queue? Have you ever thought about what will be the process to implement Priority Queue? Well, if you’re curious to know about the same then you can read our article here.
Let us first think about what is called a Priority Queue in daily life.
Letโs assume one scenario:
Suppose, you & your friends are standing in a queue for train tickets. Suddenly, you saw a very old man there. What will be your duty at that moment? Your duty to first let the old man book his ticket. That means the old man has the priority to book the ticket. Again a disabled person came to book a ticket there. Then what will you do at that point? Who will book the ticket first? The old man or the disabled person?
So, in such cases, there will be a relative priority consideration happen. Generally, a Disabled person has the top priority. After that, the old man will book the ticket. At last, you & your friends can collect the ticket.
The same thing happens to the Priority Queue in Java. Several elements will make a queue along with a special value. Among them, whoever has the highest special value will first come out from the queue. Still, have doubts? Hire a Java assignment helper to clear your doubts.
But before we implement the Priority Queue Java, let us first know briefly what do you mean by this.
ย
What Is Priority Queue in Java? Read More
ย
Not only in Java but Priority Queue can also be implemented in any programming language. It is a special type of Queue. The Queue is a type of Data Structure. Priority Queue is quite different from that. In such cases, there is a special value available called ‘Priority’. Based on the priority insertion or deletion can happen.
It has some properties. Depending upon the properties the operations are performed. They are:
- Each element is associated with a priority value. And, elements are inserted based on their priority. That is, higher priority elements are served first.
- An element with high priority is dequeued before an element with low priority.
- However, if elements with the same priority occur, they are served according to their order in the queue.
ย
ย
ย
Get to Know Types Of Priority Queue:
ย
Priority Queue Java mainly can be differentiated into two types. According to the Priority with the number, the divisions are following two types:
Minimum Priority Queue or Ascending Order Priority Queue:
ย
In this type , the minimum number gets the highest priority. On the other hand, the maximum number in the queue gets the minimum priority. That is why this type of Priority Queue appears in the Ascending Order. That is why sometimes it is called an Ascending Order Priority Queue.
ย
Suppose, in a queue, the number 1 gets the priority 10 & number 10 gets the priority 1. Same number 2 gets priority 9 & number 9 gets priority 2. So, what will be the structure of the queue? It will be in ascending order like 1,2,3,4โฆ.,10. The number with the highest priority appears at the beginning.
ย
Maximum Priority Queue or Descending Order Priority Queue:
ย
It is just the opposite of the Minimum Priority Queue. In this type of Priority Queue, the minimum number gets the lowest priority. On the other hand, the maximum number in the queue gets the highest priority. That is why this type of Priority Queue appears in the Descending Order. This is the reason it is called a Descending Order Priority Queue.
ย
Suppose, in a queue, the number 10 gets the priority 10 & number 1 gets the priority 1. Same number 9 gets priority 9 & number 2 gets priority 2. So, what will be the structure of the queue? It will be in descending order like 10,9,8,7โฆ.,1. The number with the highest priority appears at the beginning.
ย
ย
Ways To Implement Priority Queue:
ย
Though there are many ways to implement . But it is the nature of humans to follow the relatively easier rule. So, mainly there are two ways to implement a Priority Queue. Generally, a normal Queue can also be implemented using such Data Structures.
The proposed ways to implementย are:
- Using Linked List
- Using Array
Both above are types of Data Structures. Both are linear. But in between them, using Linked List to develop the Priority Queue is the easiest process. Just we need to have the basic knowledge of the Linked List.
ย
What are the Operations in Priority Queue?
ย
Not only Priority Queue, but Normal Queue also has only the following two operations. Other than these, there will not be any possible operation performed on Queue. Though developers can add any operations made by themselves. The proposed operationsย are:
- En-Queue or Insertion
- De-Queue or Deletion
Let us know briefly about the processes.
ย
ย
En-Queue Operation in Priority Queue:
ย
En-Queue is a simple operation. In the normal queue, a number will be added to the last of the Queue.ย But in the case of Priority Queue, the operation is quite different.
En-Queue operation inserts an item into it. The item can be inserted at the end of the queue or the front of the queue or in the middle based on the priority value.
ย
ย
De-Queue Operation in Priority Queue:ย
ย
De-Queue is another operation performed in the Queue. In a normal queue also, there is a De-Queue operation. But there every number was removed from a certain side of the Queue. Whereas in Priority Queue the operation is quite different.
The De-Queue operation deletes the item with the highest priority from the queue. This means it will track the priority of the numbers. And then the number with the highest priority will get removed. Same as we have given in our daily life example.
ย
How To Do Implementation ? Read Below
ย
Here, we are mainly using the Linked List method. according to the Linked List, we have taken one structure. The structure will have the info, and priority of that number. Also, it will have the pointer to store the address of the next node.
Let us implement some code snippets of important operations:
ย
- De-Queue Operation or Deletion Operation:
ย
Here, we need to first check whether the Priority Queue is empty or not. If it is empty then the Deletion canโt happen. But if it has the elements in the Queue, then the element which has the highest priority will get removed first along with the priority. Then the Starting Pointer will point to the next address.
// Function To Delete To Node From Queue void delete(){ ย ย ย // Condition To Checks If Queue Is Already Empty ย ย ย struct node *p; ย ย ย if(start==NULL) ย ย ย ย ย ย ย printf("List is Empty\n"); ย ย ย // Deleting The Node ย ย ย else { ย ย ย ย ย ย ย p=start; ย ย ย ย ย ย ย start=start->next; ย ย ย ย ย ย ย printf("The deleted node is %d\n",p->info); ย ย ย ย ย ย ย free(p); }}
ย
- En-Queue Operation or Insertion Operation:
ย
If the start pointer is equal to null then Queue is empty. Then we have to create a node and make the node at the front of the queue. Else if the front is not equal to null then iterate over the queue till its reaches a node with a priority less than the new node.
There we need to insert the data in the queue before at the position where the new node priority is greater than the priority of the element in the queue. If the priority of the new node is greater than the start node. Then the new node will be inserted in the beginning & the start node will become the next node.
// Function To Add Node In Queue According To Priority void insert(int x,int n){ ย ย ย struct node *p,*prev; ย ย ย struct node *newnode; ย ย ย // This Will Create Node In Dynamic Manner. ย ย ย newnode=(struct node *)malloc(sizeof(struct node)); ย ย ย prev=(struct node *)malloc(sizeof(struct node)); ย ย ย newnode->info=x;ย ย // Storing Given Input Info In New Node ย ย ย newnode->priority=n;ย // Storing Priority Of Node ย ย ย newnode->next=NULL;ย // Storing Address Of Next Node ย ย ย // Condition To Check If Queue Is Empty If It Is Then It Will Allot Newnode as Start/First Node ย ย ย if(start==NULL) ย ย ย ย ย ย ย start=newnode; ย ย ย //Condition To Check If Incoming Node Priority Is Lower Than Start Node ย ย ย else if(newnode->priority>start->priority){ ย ย ย ย ย ย ย prev=start;ย ย // This Will Store Start Node To Temporary Node Called As Prev ย ย ย ย ย ย ย p=start; ย ย ย ย ย ย ย // Iterate Over Nodes Till It Finds Node With Less Priority ย ย ย ย ย ย ย while(p!=NULL && p->priority<=newnode->priority){ ย ย ย ย ย ย ย ย ย ย ย prev=p; ย ย ย ย ย ย ย ย ย ย ย p=p->next;} ย ย ย ย ย ย ย newnode->next=p; ย ย ย ย ย ย ย prev->next=newnode;} ย ย ย // If Incoming Node Has Highest Priority Then Make As Start Node And Start Node As Next Node ย ย ย else{ ย ย ย ย ย ย ย newnode->next=start; ย ย ย ย ย ย start=newnode; }}
- Display Of the Queue:
ย
In this function, first, it will be checked whether the queue has elements or not. If it has elements then the pointer will iterate to every node. And it will print the data stored there. Also, it will print the priority of the numbers. // Function To Display The Queue With Priority void display(){ ย ย ย struct node *p; ย ย ย // Condition To Check if Queue is Already Empty ย ย ย if(start==NULL) ย ย ย ย ย ย ย printf("List is Empty\n"); ย ย ย // Iterate Over All Node & Simultaneously Print Its Data ย ย ย else{ ย ย ย ย ย ย ย p=start; ย ย ย ย ย ย ย printf("The elements in the link list are\n"); ย ย ย ย ย ย ย printf("PRIORITYย INFO\n"); ย ย ย ย ย ย ย while(p!=NULL) { ย ย ย ย ย ย ย ย ย ย ย printf("%d %d\n",p->priority,p->info); ย ย ย ย ย ย ย ย ย ย p=p->next;}}
- Main Function:
In this function, we will ask the users to choose their input. After that, they will provide the inputs there. The inputs will be transferred to a certain function for further process. According to the input of the user, proper action will be performed.
void main(){ ย ย ย struct node *p; int x,choice,n; ย ย ย while(1){ย // User Input Choice ย ย ย ย ย ย ย printf("Enter your choice:\n1.insert as per priority\n2.delete \n3.display\n4.exit\n"); ย ย ย ย ย ย ย scanf("%d",&choice); ย ย ย ย ย ย ย switch(choice){ case 1: ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย printf("Enter value to be entered into queue :"); scanf("%d",&x); ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย printf("Enter priority of the node :"); scanf("%d",&n); insert(x,n); break; ย ย ย ย ย ย ย ย ย ย ย case 2: delete(); break; case 3: display(); break; ย ย ย ย ย ย ย ย ย ย ย case 4: printf("Thank you\n"); exit(0); break; ย ย ย ย ย ย ย ย ย ย ย default: printf("Invalid input\n");}}}
Above are the code snippets. If needed, you can go through the full source code provided here. The probable output of theย will be the following.
Output:
ย | |
ย | ย |
Conclusion:
ย
As we saw Priority Queue is very important.
We should clear the basics of recursion & function calling for learning the Priority Queue.
We should clear the Linked List concept also. That is very important to implement the same.
So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.
If you are looking for Final Yearย Java Project Ideasย then you can contact us as well. If you are curious to learn other topics on Java then we have writtenย some useful and detailed articles on the below topics:
- Learn about throwing exception in Java
- Knowย what is multithreading in Java?
- Get toย know how to reverse list in Java
- Have you wondered aboutย the difference between comparable vs comparator
- Learnย how to compare two strings in Java
- Know aboutย printing an array in Java