Priority Queue Introduction
What is a Priority Queue in Java ?
A Priority Queue in java programming is a data structure, in which priority is set already . In priority queue we work with the data depending on some priority .For example if we want to delete an element from the queue, the data with the highest priority will be deleted first, in similar way if we are inserting data in the queue, it will be arranged in the queue depending upon its priority.
Let’s see how a priority queue in java works.
Priorty Queue introduction.
Priority Queue is other form of normal queue with following properties.
- Each and Every item has a priority associated with it.
- An element which has high priority is dequeued first as compared with element with low priority.
- If by chance elements have the same priority, they are served according to their order in the queue.
- A Priority Queue can be implemented using various different data structures like
- Arrays
- Linked List
- Heap
Priority Queue Operation
- Enqueue(): This function is for inserting an element in a queue
- Dequeue(): This function is used for deleting an element in a queue, based on the priority of the element.
- show(): This will print all elements of the Queue.
How is a Priority Queue implemented in Java ?
There are three different data structures that we can use to implement Priority Queue in java:
- Array
- Linked List
- Heap
Java code to implement priority queue using queue class
// Java program to implement priority queue // using queue class import java.util.*; public class Main { public static void main(String[] args) { // Creating a priority queue of strings PriorityQueue< String> pq = new PriorityQueue<>(); pq.add("hello"); pq.add("hiii"); pq.add("by"); System.out.println("Priority Queue is: " + pq); String value = pq.peek(); System.out.println("Element accessed is: " + value); } }
Output : Priority Queue is : [by, hiii, hello] Element accessed is : by
APPLICATIONS OF PRIORITY QUEUES :
Following are some applications of Priority Queue:
- Dijkstra’s shortest path algorithm.
- Data Compression
- Heap Sort
- Prim’s Algorithm
- Operating System: Interrupt Handling.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment