Priority Queue Insertion and Deletion in Java

priority queue insertion and deletion

Priority Queue Insertion and Deletion

Normal queue follows a First In First Out (FIFO) order to insert and remove an element. 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. On this page we will discuss priority queue insertion and deletion in java.

Priority Queue Insertion And Deletion

Introduction

A priority is basically an extension of the general queue. A major difference is that dequeuing happens based on the priority of each item in the queue.

We have two types of priority queue –

  • Max Priority Queue – A larger priority value means higher priority
  • Min Priority Queue – A smaller priority value means lower priority

By default, if nothing is mentioned it means that the Priority queue is a min priority queue.

A Priority Queue can be implemented using –

  • Arrays
  • Linked List
  • Heaps

You can check these pages here for implementation using Linked List and arrays

We will be discussing the array approach here.

Operations

  • enqueue(): To insert a new item at the end of the queue.
  • dequeue(): To remove the element with the highest priority from the queue.
  • peek()/top(): To get the highest priority element in the queue without dequeuing the item.
priority queue insertion
priority queue deletion

Java code for priority queue insertion and deletion

Run
// Java program for insertion and deletion in priority queue
// using queue class

import java.util.*;

class Main{

    // Main Method

    public static void main(String[] args)

    {

        // Creating a priority queue

        PriorityQueue pq = new PriorityQueue<>();

        pq.add("Insta");

        pq.add("Prep");

        pq.add("Prime");

        System.out.println("Priority Queue is : " + pq);

        System.out.println("Element accessed is : " + pq.remove());
        System.out.println("Element accessed is : " + pq.remove());

    }
}
Output :
Priority Queue is : [Insta, Prep, Prime]
Element accessed is : Insta
Element accessed is : Prep

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java