C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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.

priority queue in data structure

Image source: https://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<stdio.h>
# include<malloc.h>

typedef struct node
	int priority;
	int info;
	struct node *link;
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 < front->priority )
		tmp->link = front;
		front = tmp;
		q = front;
		while( q->link != NULL && q->link->priority <= priority )
		tmp->link = q->link;
		q->link = tmp;
/*End of insert*/

/*Begin of del*/
void del()
	NODE *tmp;
	if(front == NULL)
		printf("Queue Underflow\n");
		tmp = front;
		printf("Deleted item is %d\n",tmp->info);
		front = front->link;
/*End of del*/

/*Begin of display*/
void display()
	NODE *ptr;
	ptr = front;
	if(front == NULL)
		printf("Queue is empty\n");
		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;
		printf("Enter your choice : ");
		scanf("%d", &choice);
			case 1:
				printf("Input the item value to be added in the queue : ");
				printf("Enter its priority : ");
			case 2:
			case 3:
			case 4:
				default :
				printf("Wrong choice\n");

	return 0;
/*End of main*/


priority queue in data structure

Liked this article? Do share with your friends :)