Priority queue using linked list in C++

Home » Data Structure

Implementation of priority queue using linked list

In this article, we are going to learn how to implement a priority queue using C language with the linked list in the data structure?
Submitted by Manu Jemini, on December 21, 2017

A priority queue is a very important data structure because it can store data in a very practical way. This is a concept of storing the item with its priority. This way we can prioritize our concept of a queue.


Image source: //netmatze.files.wordpress.com/2014/08/priorityqueue.png

In the Code below there are four parts. First three function to implement three different operations like Insert a node, delete a node and display the list. The Fourth part is the main function, in that a do while loop is implemented to keep the user engaged and provide him the all the given choices, according to the choice one of the three function get called.

The First function creates a new node and then checks whether the queue is empty or not. If the queue is empty it adds the node at the first place, else it reaches to the last position and adds the item there.

The Second function checks whether the list is empty, or it is not. If any node exists, it will delete the node.

The Third function will simply print all the elements of the Queue if exist. If not, then it will say Queue is Empty.

C program to implement priority queue

# include # include typedef struct node { int priority; int info; struct node *link; }NODE; NODE *front = NULL; /*Begin of insert*/ void insert[int item,int priority] { NODE *tmp,*q; tmp = [NODE *]malloc[sizeof[NODE]]; tmp->info = item; tmp->priority = priority; /*Queue is empty or item to be added has priority more than first item*/ if[ front == NULL || priority priority ] { tmp->link = front; front = tmp; } else { q = front; while[ q->link != NULL && q->link->priority link; tmp->link = q->link; q->link = tmp; } } /*End of insert*/ /*Begin of del*/ void del[] { NODE *tmp; if[front == NULL] printf["Queue Underflow\n"]; else { tmp = front; printf["Deleted item is %d\n",tmp->info]; front = front->link; free[tmp]; } } /*End of del*/ /*Begin of display*/ void display[] { NODE *ptr; ptr = front; if[front == NULL] printf["Queue is empty\n"]; else { printf["Queue is :\n"]; printf["Priority Item\n"]; while[ptr != NULL] { printf["%5d %5d\n",ptr->priority,ptr->info]; ptr = ptr->link; } } } /*End of display*/ /*Begin of main*/ int main[] { int choice,item,priority; do { printf["1.Insert\n"]; printf["2.Delete\n"]; printf["3.Display\n"]; printf["4.Quit\n"]; printf["Enter your choice : "]; scanf["%d", &choice]; switch[choice] { case 1: printf["Input the item value to be added in the queue : "]; scanf["%d",&item]; printf["Enter its priority : "]; scanf["%d",&priority]; insert[item,priority]; break; case 2: del[]; break; case 3: display[]; break; case 4: break; default : printf["Wrong choice\n"]; } }while[choice!=4]; return 0; } /*End of main*/

Output



ADVERTISEMENT


What's New!

  • AngularJS Multiple-Choice Questions [MCQs]
  • JSON Multiple-Choice Questions [MCQs]
  • Ajax Multiple-Choice Questions [MCQs]
  • SASS Multiple-Choice Questions [MCQs]
  • HTML Multiple-Choice Questions [MCQs]
  • Advanced CSS Multiple-Choice Questions [MCQs]
  • CSS Multiple-Choice Questions [MCQs]
  • MS Word Multiple-Choice Questions [MCQs]
  • MIS Executive Interview Questions and Answers
  • Go Language Interview Questions and Answers
  • PL/SQL Multiple-Choice Questions [MCQs]
  • SQL Multiple-Choice Questions [MCQs]
  • Software Engineering MCQs
  • MIS MCQs
ADVERTISEMENT

Top Interview Coding Problems/Challenges!

  • Run-length encoding [find/print frequency of letters in a string]
  • Sort an array of 0's, 1's and 2's in linear time complexity
  • Checking Anagrams [check whether two string is anagrams or not]
  • Relative sorting algorithm
  • Finding subarray with given sum
  • Find the level in a binary tree with given sum K
  • Check whether a Binary Tree is BST [Binary Search Tree] or not
  • 1[0]1 Pattern Count
  • Capitalize first and last letter of each word in a line
  • Print vertical sum of a binary tree
  • Print Boundary Sum of a Binary Tree
  • Reverse a single linked list
  • Greedy Strategy to solve major algorithm problems
  • Job sequencing problem
  • Root to leaf Path Sum
  • Exit Point in a Matrix
  • Find length of loop in a linked list
  • Toppers of Class
  • Print All Nodes that don't have Sibling
  • Transform to Sum Tree
  • Shortest Source to Destination Path
ADVERTISEMENT


Comments and Discussions!

Please enable JavaScript to view the comments powered by Disqus.
ADVERTISEMENT

Video liên quan

Chủ Đề