A priority queue is a data structure where each element is served according to its priority rather than just the order it arrived. A multiple priority queue implements this with several separate queues — one per priority level — so that higher-priority items are always removed before lower-priority ones. This C program builds a simple menu-driven multiple priority queue with three priority levels using arrays.
How It Works
We keep an array of three queue structures (priority 1 is highest). Inserting an element adds it to the queue for its priority. Deleting always removes from the highest-priority non-empty queue first, then the next, and so on.
The Program
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 /* capacity of each priority queue */
#define LEVELS 3 /* number of priority levels */
struct pqueue {
int front, rear;
int items[MAX];
};
int is_empty(struct pqueue *q) { return q->front > q->rear; }
int is_full(struct pqueue *q) { return q->rear == MAX - 1; }
void insert(struct pqueue *q)
{
int x;
if (is_full(q)) {
printf("Queue overflow.\n");
return;
}
printf("Enter the element to insert : ");
scanf("%d", &x);
q->items[++(q->rear)] = x;
}
void delete_item(struct pqueue q[])
{
for (int i = 0; i < LEVELS; i++)
if (!is_empty(&q[i])) {
printf("Deleted %d (from priority %d)\n",
q[i].items[q[i].front++], i + 1);
return;
}
printf("All queues are empty.\n");
}
void display(struct pqueue q[])
{
for (int i = 0; i < LEVELS; i++) {
printf("Priority %d : ", i + 1);
if (is_empty(&q[i]))
printf("(empty)");
else
for (int j = q[i].front; j <= q[i].rear; j++)
printf("%d ", q[i].items[j]);
printf("\n");
}
}
int main(void)
{
struct pqueue q[LEVELS];
int ch, p;
for (int i = 0; i < LEVELS; i++) {
q[i].front = 0;
q[i].rear = -1;
}
while (1) {
printf("\n--- MENU ---\n1.Insert 2.Delete 3.Display 4.Exit\n");
printf("Enter your choice : ");
if (scanf("%d", &ch) != 1)
break;
switch (ch) {
case 1:
printf("Enter priority (1-%d) : ", LEVELS);
scanf("%d", &p);
if (p < 1 || p > LEVELS) {
printf("Invalid priority.\n");
break;
}
insert(&q[p - 1]);
break;
case 2: delete_item(q); break;
case 3: display(q); break;
case 4: exit(0);
default: printf("Invalid choice.\n");
}
}
return 0;
}
How the Program Works
- Each priority level is a fixed-size array queue with its own
frontandrearindices. - Insert asks for a priority (1–3) and appends the element to that queue, guarding against overflow.
- Delete scans the queues from highest to lowest priority and removes from the first non-empty one — this is what gives the structure its priority behaviour.
- Display prints the contents of all three queues.
- This version cleans up the original implicit-
int main(), the missing newline escapes (ninstead of\n), and a stray character, and adds input validation.
Sample Output
--- MENU --- 1.Insert 2.Delete 3.Display 4.Exit Enter your choice : 1 Enter priority (1-3) : 2 Enter the element to insert : 50 Enter your choice : 2 Deleted 50 (from priority 2)
For a thorough treatment of queues and other data structures in C, The C Programming Language by Kernighan and Ritchie is the classic reference — find it on Amazon.
This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.
Related C Programs
Try it instantly in one of the best online C compilers, or set up a local toolchain with our complete C development environment guide.