Implementation of Priority Queue Using Linked List in C++

Priority Queue using linked list in C++

In this section we will implement the priority queue using linked list. Priority Queue implementation using linked list is more efficient than using array. As Linked list provide you the facility of Dynamic memory allocation .If we use Linked List for implementing Priority Queue then memory is not going to waste. We can free the memory which is not in use anymore.

Implementation of priority queue using linked list in C++

Algorithm for Priority Queue Implementation using Linked List: 

  • Create a node structure for creating nodes of the linked list.
  • insert() : This function is used to insert the elements based on the priority level in linked list.
  • delete() : This function delete the head node of the linked list, and make free that memory and then the next node will become the head node of                         the linked list.
  • display() : This function is used to display the elements of the priority queue that is implemented using linked list.
  • peak() : This function is used to display the top element of the priority queue. 
Implementation of priority queue using linked list in C

Code Implementation for Priority Queue using Linked List

Run
#include<bits/stdc++.h>
using namespace std;
struct Node
{
  int data;
  int priority;
  Node *next;
};

Node *front = NULL;

void insert (int data, int priority)
{

  Node *temp, *curr, *pre = NULL;

  temp = new Node;
  temp->data = data;
  temp->priority = priority;

  if (front == NULL or priority >= front->priority)
    {
      temp->next = front;
      front = temp;
    }

  else
    {
      curr = front;

      while (curr and priority <= curr->priority)
	{
	  pre = curr;
	  curr = curr->next;
	}

      temp->next = pre->next;
      pre->next = temp;
    }
}

void Delete ()
{
  if (front == NULL)
    {
      cout << "Priority Queue is underflow" << endl;
      return;
    }

  else
    {
      Node *temp;
      temp = front;

      cout << "Deleted item is " << temp->data << endl;

      front = temp->next;
      free (temp);
    }
}

void display ()
{
  if (front == NULL)
    cout << "Priority-Queue is empty" << endl;

  Node *curr = front;

  cout << "\nPriority-Queue elements are : ";

  while (curr)
    {
      cout << curr->data << " ";
      curr = curr->next;
    }

  cout << endl;

  return;
}

void peak ()
{
  cout << "Peak element is :" << front->data << endl;
}

int main ()
{
  insert (2, 9);
  insert (3, 4);
  insert (6, 8);

  display ();

  peak ();

  Delete ();
  Delete ();
  display ();

  return 0;

}
Output:

Priority-Queue elements are :2 6 3

Peak element is : 2

Deleted element : 2

Deleted element : 6

Priority-Queue elements are : 3



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