You are on page 1of 1

#include <stdio.h> #include <conio.

h> #define max 10


int rear = 0, front = 0; int queue[20]; int priorities[20]; // Array to store priori-
ties
void insertion(); void deletion(); void display();
void main() { int ch; char op; clrscr(); printf(”\n Priority Queue \n”); printf(”1.
Enqueue\n”); printf(”2. Dequeue\n”); printf(”3. Display\n”); printf(”4.
Exit\n”); do { printf(”\nEnter your choice: ”); scanf(”%d”, &ch); switch
(ch) { case 1: insertion(); break; case 2: deletion(); break; case 3: display();
break; case 4: exit(0); default: printf(”\nInvalid choice, please enter a correct
choice.”); } printf(”\nDo you want to continue (y/n)? ”); fflush(stdin); scanf(”
%c”, &op); } while (op == ’y’ || op == ’Y’); getch(); }
void insertion() { int item, priority; if (rear == max) { printf(”Queue over-
flow\n”); return; } else { printf(”Enter an element for insertion: ”); scanf(”%d”,
&item); printf(”Enter the priority for the element: ”); scanf(”%d”, &priority);
// Find the position to insert based on priority int pos = rear; while (pos > front
&& priority > priorities[pos - 1]) { queue[pos] = queue[pos - 1]; priorities[pos]
= priorities[pos - 1]; pos--; }
queue[pos] = item; priorities[pos] = priority; rear++; } }
void deletion() { if (front == rear) { printf(”Queue underflow\n”); return; } else
{ int item = queue[front]; front++; printf(”Deleted element = %d\n”, item); }
}
void display() { if (front == rear) { printf(”Queue is empty\n”); } else
{ printf(”Elements of the queue:\n”); for (int i = front; i < rear; i++) {
printf(”(%d, priority %d) ”, queue[i], priorities[i]); } printf(”\n”); } }

You might also like