What is a Priority Queue?
A priority queue is a special type of queue where:
-
Each element is associated with a priority.
-
Higher priority elements are served before lower priority elements.
-
If two elements have the same priority, they are served according to their order of insertion (just like a normal queue).
Example:
Imagine an emergency room:
How to Implement Priority Queue using Linked List?
We use a linked list where:
Algorithms for Priority Queue using Linked List:
1. Node Structure
2. Enqueue (Insert according to priority)
Algorithm:
-
Create a new node with given data and priority.
-
If queue is empty OR the new node's priority is higher than the front node's priority:
-
Else:

3. Dequeue (Remove Highest Priority)
Algorithm:
-
If the queue is empty:
-
Else:
4. Display Queue
Algorithm:
-
Start from the front.
-
Print each node's data and priority.
Operation | Time Complexity | Notes |
---|
Enqueue | O(n) | Must find correct place |
Dequeue | O(1) | Always remove from front |
Example-1:
A placement drive interview is scheduled based on the marks in written test(out of 100).Person with highest mark is considered for interview.Create a priority queue and schedule the interview.
C Program- Priority Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char name[50];
int marks;
struct Node* next;
};
typedef struct Node Node;
Node* front = NULL;
// Function to create a new node
Node* createNode(char name[], int marks) {
Node* temp = (Node*)malloc(sizeof(Node));
strcpy(temp->name, name);
temp->marks = marks;
temp->next = NULL;
return temp;
}
// Function to insert candidate into the priority queue
void insert(char name[], int marks) {
Node* newNode = createNode(name, marks);
if (front == NULL || marks > front->marks) {
newNode->next = front;
front = newNode;
} else {
Node* current = front;
while (current->next != NULL && current->next->marks >= marks) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
printf("Candidate %s with marks %d added to the queue.\n", name, marks);
}
// Function to schedule interview (delete highest priority)
void scheduleInterview() {
if (front == NULL) {
printf("No candidates in the queue.\n");
return;
}
Node* temp = front;
printf("Interview scheduled for: %s with marks %d\n", temp->name, temp->marks);
front = front->next;
free(temp);
}
// Function to display the queue
void displayQueue() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("\nCurrent Interview Queue (Highest Marks First):\n");
Node* temp = front;
while (temp != NULL) {
printf("Name: %-20s Marks: %d\n", temp->name, temp->marks);
temp = temp->next;
}
}
// Main menu
int main() {
int choice, marks;
char name[50];
while (1) {
printf("\n--- Placement Interview Scheduler ---\n");
printf("1. Insert Candidate\n");
printf("2. Schedule Interview\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // consume newline
switch (choice) {
case 1:
printf("Enter Candidate Name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0; // remove newline
printf("Enter Marks (out of 100): ");
scanf("%d", &marks);
insert(name, marks);
break;
case 2:
scheduleInterview();
break;
case 3:
displayQueue();
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Example-2:
The General post office wishes to give preferential treatment to its customers. They have identified the customer categories as Defence personnel, Differently abled,Senior citizen, Ordinary. The customers are to be given preference in the decreasing order - Differently abled, Senior citizen, Defence personnel,Normal person.Generate the possible sequence of completion.
C Program- Priority Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define node
struct Node {
char name[100];
int priority;
struct Node* next;
};
struct Node* front = NULL;
// Create a new node
struct Node* createNode(char name[], int priority) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
strcpy(temp->name, name);
temp->priority = priority;
temp->next = NULL;
return temp;
}
// Insert based on priority
void enqueue(char name[], int priority) {
struct Node* newNode = createNode(name, priority);
if (front == NULL || priority > front->priority) {
newNode->next = front;
front = newNode;
} else {
struct Node* temp = front;
while (temp->next != NULL && temp->next->priority >= priority) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
printf("Customer %s added with priority %d\n", name, priority);
}
// Remove from front
void dequeue() {
if (front == NULL) {
printf("No customers to serve.\n");
} else {
struct Node* temp = front;
printf("Serving Customer: %s (Priority: %d)\n", front->name, front->priority);
front = front->next;
free(temp);
}
}
// Display queue
void displayQueue() {
struct Node* temp = front;
if (temp == NULL) {
printf("No customers in queue.\n");
} else {
printf("\nQueue:\n");
while (temp != NULL) {
printf("%s (Priority: %d) -> ", temp->name, temp->priority);
temp = temp->next;
}
printf("NULL\n");
}
}
int main() {
int choice;
char name[100], category[100];
int priority;
do {
printf("\nMenu\n----\n");
printf("1. Add Customer\n");
printf("2. Serve Customer\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // to consume leftover newline
switch(choice) {
case 1:
printf("Enter customer name: ");
gets(name);
printf("Enter customer category(Differently Abled-4/Senior Citizen-3/Defence Personnel-2/Ordinary-1): ");
scanf("%d",&priority);
enqueue(name, priority);
displayQueue();
break;
case 2:
dequeue();
displayQueue();
break;
case 3:
displayQueue();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while(choice != 4);
return 0;
}
Comments
Post a Comment