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.
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.
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
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