
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Priority Queue using Linked List in C
We are given with the data and the priority as an integer value and the task is to create a 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 linked list for implementing priority queue will contain three parts −
- Data − It will store the integer value.
- Address − It will store the address of a next 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 data, priority Declare a struct node* next Step 2-> In function Node* newNode(int d, int p) Set Node* temp = (Node*)malloc(sizeof(Node)) Set temp->data = d Set temp->priority = p Set temp->next = NULL Return temp Step 3-> In function int peek(Node** head) return (*head)->data Step 4-> In function void pop(Node** head) Set Node* temp = *head Set (*head) = (*head)->next free(temp) Step 5-> In function push(Node** head, int d, int p) Set Node* start = (*head) Set Node* temp = newNode(d, p) If (*head)->priority > p then, Set temp->next = *head Set (*head) = temp Else Loop While start->next != NULL && start->next->priority < p Set start = start->next Set temp->next = start->next Set start->next = temp Step 6-> In function int isEmpty(Node** head) Return (*head) == NULL Step 7-> In function int main() Set Node* pq = newNode(7, 1) Call function push(&pq, 1, 2) Call function push(&pq, 3, 3) Call function push(&pq, 2, 0) Loop While (!isEmpty(&pq)) Print the results obtained from peek(&pq) Call function pop(&pq) Stop
Example
#include <stdio.h> #include <stdlib.h> // priority Node typedef struct node { int data; int priority; struct node* next; } Node; Node* newNode(int d, int p) { Node* temp = (Node*)malloc(sizeof(Node)); temp->data = d; temp->priority = p; temp->next = NULL; return temp; } int peek(Node** head) { return (*head)->data; } void pop(Node** head) { Node* temp = *head; (*head) = (*head)->next; free(temp); } void push(Node** head, int d, int p) { Node* start = (*head); Node* temp = newNode(d, p); if ((*head)->priority > p) { temp->next = *head; (*head) = temp; } else { while (start->next != NULL && start->next->priority < p) { start = start->next; } // Either at the ends of the list // or at required position temp->next = start->next; start->next = temp; } } // Function to check the queue is empty int isEmpty(Node** head) { return (*head) == NULL; } // main function int main() { Node* pq = newNode(7, 1); push(&pq, 1, 2); push(&pq, 3, 3); push(&pq, 2, 0); while (!isEmpty(&pq)) { printf("%d ", peek(&pq)); pop(&pq); } return 0; }
Output
2 7 1 3
- Related Articles
- Priority Queue using doubly linked list in C++
- C++ Program to Implement Queue using Linked List
- Explain queue by using linked list in C language
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- Dequeue and Priority Queue in C++
- Priority Queue Introduction in C/C++\n
- Creating a Priority Queue using Javascript
- Double ended priority queue in C++ Program
- C++ Program to Implement Priority Queue
- Turn a Queue into Priority Queue
- Python Program to Implement Queue Data Structure using Linked List
- Priority Queue in C++ Standard Template Library (STL)
- Multithreaded Priority Queue in Python
- The Priority Queue in Javascript
- Meldable Priority Queue Operations

Advertisements