| #include <stdio.h>
|
| #include <stdlib.h>
|
|
|
| typedef struct Node { int data; int prio; struct Node* next; } Node;
|
| Node* head = NULL;
|
|
|
| void insert(int d, int p) {
|
| Node* t = (Node*)malloc(sizeof(Node)); t->data = d; t->prio = p;
|
| if (!head || head->prio < p) { t->next = head; head = t; }
|
| else {
|
| Node* curr = head;
|
| while (curr->next && curr->next->prio >= p) curr = curr->next;
|
| t->next = curr->next; curr->next = t;
|
| }
|
| }
|
|
|
| void pop() {
|
| if(!head) { printf("Empty\n"); return; }
|
| Node* t = head; printf("Served: %d\n", t->data); head = head->next; free(t);
|
| }
|
|
|
| // New Display Function
|
| void display() {
|
| if (!head) {
|
| printf("Queue is Empty\n");
|
| return;
|
| }
|
| Node* curr = head;
|
| printf("Priority Queue: \n");
|
| printf("[Data, Priority] -> ");
|
| while (curr != NULL) {
|
| printf("[%d, P:%d]", curr->data, curr->prio);
|
| if (curr->next != NULL) printf(" -> ");
|
| curr = curr->next;
|
| }
|
| printf("\n");
|
| }
|
|
|
| int main() {
|
| int choice, d, p;
|
| while(1) {
|
| printf("\n1. Insert (Value & Priority)\n2. Dequeue\n3. Display\n4. Exit\nEnter choice: ");
|
| scanf("%d", &choice);
|
|
|
| if(choice == 1) {
|
| printf("Enter Value and Priority (Higher number = Higher Priority): ");
|
| scanf("%d %d", &d, &p);
|
| insert(d, p);
|
| }
|
| else if(choice == 2) pop();
|
| else if(choice == 3) display();
|
| else break;
|
| }
|
| return 0;
|
| }
|