Priority Queue using doubly linked list in C++


We are given with the data and the priority as an integer value and the task is to create a doubly linked list as per the priority given and display the result.

Queue is a FIFO data structure in which the element which is inserted first is the first one to get removed. A Priority Queue is a type of queue in which elements can be inserted or deleted depending upon the priority. It can be implemented using queue, stack or linked list data structure. Priority queue is implemented by following these rules −

  • Data or element with the highest priority will get executed before the data or element with the lowest priority.
  • If two elements have the same priority than they will be executed in the sequence they are added in the list.

A node of a doubly linked list for implementing priority queue will contain three parts −

  • Data − It will store the integer value.
  • Next Address − It will store the address of a next node
  • Previous Address − It will store the address of a previous node
  • Priority − It will store the priority which is an integer value. It can range from 0-10 where 0 represents the highest priority and 10 represents the lowest priority.

Example

Input -

 

Output- 

Algorithm

Start
Step 1-> Declare a struct Node
   Declare info, priority
   Declare struct Node *prev, *next
Step 2-> In function push(Node** fr, Node** rr, int n, int p)
   Set Node* news = (Node*)malloc(sizeof(Node))
   Set news->info = n
   Set news->priority = p
   If *fr == NULL then,
      Set *fr = news
      Set *rr = news
      Set news->next = NULL
   Else If p <= (*fr)->priority then,
      Set news->next = *fr
      Set (*fr)->prev = news->next
      Set *fr = news
   Else If p > (*rr)->priority then,
      Set news->next = NULL
      Set (*rr)->next = news
      Set news->prev = (*rr)->next
      Set *rr = news
   Else
      Set Node* start = (*fr)->next
   Loop While start->priority > p
      Set start = start->next
      Set (start->prev)->next = news
      Set news->next = start->prev
      Set news->prev = (start->prev)->next
      Set start->prev = news->next
Step 3-> In function int peek(Node *fr)
   Return fr->info
Step 4-> In function bool isEmpty(Node *fr)
   Return (fr == NULL)
Step 5-> In function int pop(Node** fr, Node** rr)
   Set Node* temp = *fr
   Set res = temp->info
   Set (*fr) = (*fr)->next
   free(temp)
   If *fr == NULL then,
      *rr = NULL
   Return res
Step 6-> In function int main()
   Declare and assign Node *front = NULL, *rear = NULL
   Call function push(&front, &rear, 4, 3)
   Call function push(&front, &rear, 3, 2)
   Call function push(&front, &rear, 5, 2)
   Call function push(&front, &rear, 5, 7)
   Call function push(&front, &rear, 2, 6)
   Call function push(&front, &rear, 1, 4)
   Print the results obtained from calling the function pop(&front, &rear)
   Print the results obtained from calling the function peek(front)
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//doubly linked list node
struct Node {
   int info;
   int priority;
   struct Node *prev, *next;
};
//inserting a new Node
void push(Node** fr, Node** rr, int n, int p) {
   Node* news = (Node*)malloc(sizeof(Node));
   news->info = n;
   news->priority = p;
   // if the linked list is empty
   if (*fr == NULL) {
      *fr = news;
      *rr = news;
      news->next = NULL;
   } else {
      // If p is less than or equal front
      // node's priority, then insert the node
      // at front.
      if (p <= (*fr)->priority) {
         news->next = *fr;
         (*fr)->prev = news->next;
         *fr = news;
      } else if (p > (*rr)->priority) {
         news->next = NULL;
         (*rr)->next = news;
         news->prev = (*rr)->next;
         *rr = news;
      } else {
         // Finding the position where we need to
         // insert the new node.
         Node* start = (*fr)->next;
         while (start->priority > p)
         start = start->next;
         (start->prev)->next = news;
         news->next = start->prev;
         news->prev = (start->prev)->next;
         start->prev = news->next;
      }
   }
}
//the last value
int peek(Node *fr) {
   return fr->info;
}
bool isEmpty(Node *fr) {
   return (fr == NULL);
}
int pop(Node** fr, Node** rr) {
   Node* temp = *fr;
   int res = temp->info;
   (*fr) = (*fr)->next;
   free(temp);
   if (*fr == NULL)
   *rr = NULL;
   return res;
}
// main function
int main() {
   Node *front = NULL, *rear = NULL;
   push(&front, &rear, 4, 3);
   push(&front, &rear, 3, 2);
   push(&front, &rear, 5, 2);
   push(&front, &rear, 5, 7);
   push(&front, &rear, 2, 6);
   push(&front, &rear, 1, 4);
   printf("%d\n", pop(&front, &rear));
   printf("%d\n", peek(front));
   return 0;
}

Output

5
3

Updated on: 23-Dec-2019

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements