18.11.25

26. Priority Queue Using Linked List The One Where Friends Let “Priority Decide Everything”

 

hello everyone

So today’s coding session wasn’t normal because Aarav wanted everything to be first, Rahul wanted only high-priority items, and I (Daksh) just watched them fight while writing the program  And that’s exactly how we ended up learning Priority Queue using Linked List.

What’s Going On Here?

A Priority Queue is like real life the most important (or smallest numbered priority) thing gets done first. Not like Aarav, who thinks he should always go first even when his priority is 100 .

We use a linked list so that:

  • each node stores a value and its priority,

  • new elements are inserted in sorted priority order,

  • deletion always removes the highest priority element,

  • and the list grows easily using malloc.

How It Works

Rahul took over insertions he made sure every new element was placed exactly at the right priority spot, and honestly he was too strict about it “Low priority? Back of the line!” he kept saying. Aarav handled deletions and of course, he only removed the one with the highest priority, bragging every time he “deleted responsibly” I (Daksh) kept displaying the queue after each operation, like: “Here’s your Priority Queue, sorted and drama-free unlike our group decisions.” With all three of us, the priority queue behaved perfectly more perfectly than our plans ever do 

Code:

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

 struct node {
     int dataValue;
     int priorityValue;
     struct node *nextNode;
 };

 struct node *frontNode = NULL;

 void insertElement(int value, int priority) {
     struct node *temporaryNode = (struct node*)malloc(sizeof(struct node));
     temporaryNode->dataValue = value;
     temporaryNode->priorityValue = priority;
     temporaryNode->nextNode = NULL;

     if (frontNode == NULL || priority < frontNode->priorityValue) {
         temporaryNode->nextNode = frontNode;
         frontNode = temporaryNode;
     } else {
         struct node *currentNode = frontNode;
         while (currentNode->nextNode != NULL &&
                currentNode->nextNode->priorityValue <= priority) {
             currentNode = currentNode->nextNode;
         }
         temporaryNode->nextNode = currentNode->nextNode;
         currentNode->nextNode = temporaryNode;
     }
     printf("Inserted %d with priority %d\n", value, priority);
 }

 void deleteElement() {
     if (frontNode == NULL) {
         printf("Queue Underflow! No element to delete.\n");
         return;
     }
     struct node *temporaryNode = frontNode;
     printf("Deleted %d (Priority %d)\n",
            temporaryNode->dataValue,
            temporaryNode->priorityValue);
     frontNode = frontNode->nextNode;
     free(temporaryNode);
 }

 void displayQueue() {
     if (frontNode == NULL) {
         printf("Queue is empty.\n");
         return;
     }
     struct node *currentNode = frontNode;
     printf("Priority Queue: ");
     while (currentNode != NULL) {
         printf("(%d, P=%d) ",
                currentNode->dataValue,
                currentNode->priorityValue);
         currentNode = currentNode->nextNode;
     }
     printf("\n");
 }

 int main() {
     int userChoice;
     int elementValue;
     int elementPriority;
     char userDecision;

     do {
         printf("\n1. Insert\n2. Delete\n3. Display\nEnter your choice: ");
         scanf("%d", &userChoice);

         switch (userChoice) {
             case 1:
                 printf("Enter value to insert: ");
                 scanf("%d", &elementValue);
                 printf("Enter priority: ");
                 scanf("%d", &elementPriority);
                 insertElement(elementValue, elementPriority);
                 break;

             case 2:
                 deleteElement();
                 break;

             case 3:
                 displayQueue();
                 break;

             default:
                 printf("Invalid choice.\n");
         }

         printf("Do you want to continue? (y/n): ");
         scanf(" %c", &userDecision);

     } while (userDecision == 'y' || userDecision == 'Y');

     printf("Program ended.\n");
     return 0;
    }

Output:



  

← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...