Priority Queue using Linked List in C

A priority queue is a special type of queue where elements are served based on their priority rather than the order of insertion. In C, we can implement a priority queue using a linked list where each node contains data, priority, and a pointer to the next node.

In a priority queue, elements with higher priority (lower numerical value) are processed first. If two elements have the same priority, they follow FIFO order.

Syntax

typedef struct node {
    int data;
    int priority;
    struct node* next;
} Node;

Node Structure

Each node in the priority queue linked list contains −

  • Data − Stores the integer value
  • Priority − Stores priority value (0 = highest priority, higher numbers = lower priority)
  • Next − Pointer to the next node

Example: Complete Priority Queue Implementation

Here's a complete implementation of priority queue using linked list −

#include <stdio.h>
#include <stdlib.h>

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 node has lower priority than new node */
    if ((*head)->priority > p) {
        temp->next = *head;
        (*head) = temp;
    } else {
        /* Traverse the list and find correct position */
        while (start->next != NULL && start->next->priority < p) {
            start = start->next;
        }
        /* Insert at correct position */
        temp->next = start->next;
        start->next = temp;
    }
}

int isEmpty(Node** head) {
    return (*head) == NULL;
}

void display(Node* head) {
    printf("Priority Queue: ");
    while (head != NULL) {
        printf("(%d, P%d) ", head->data, head->priority);
        head = head->next;
    }
    printf("<br>");
}

int main() {
    Node* pq = newNode(7, 1);
    
    push(&pq, 1, 2);
    push(&pq, 3, 3);
    push(&pq, 2, 0);
    
    printf("Elements in priority order: ");
    while (!isEmpty(&pq)) {
        printf("%d ", peek(&pq));
        pop(&pq);
    }
    printf("<br>");
    
    return 0;
}
Elements in priority order: 2 7 1 3

How It Works

The priority queue maintains elements in sorted order based on priority −

  • Push Operation − Inserts new element at correct position based on priority
  • Pop Operation − Removes highest priority element (head of list)
  • Peek Operation − Returns highest priority element without removing it

Time Complexity

Operation Time Complexity
Push O(n)
Pop O(1)
Peek O(1)

Conclusion

Priority queue using linked list provides efficient insertion and deletion based on priority. While insertion takes O(n) time to find the correct position, removal of highest priority element is constant time O(1).

Updated on: 2026-03-15T12:29:52+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements