Priority queue using array in C
In this post, you will learn how to implement a Priority Queue using an array in the C programming language. Priority Queue implementation using an array is one of the basic methods to implement a queue. In this element, it is inserted and deleted based on its priority. The elements in the priority queue have some priority. The priority of the element is used to determine the order in which the elements will be processed.
These are the rules for processing elements in a priority queue.
- The element with the highest priority is processed first.
- The two elements with equal priority are processed based on a First Come First Serve(FCFS) basis.
The priority of the elements is set based on several factors. This is basically used in the operating system to execute the most priority process first. If this is set on the CPU time, then the element with the lowest execution time is executed first. Suppose there are two processes in a queue, one has 5ns execution time and the other has 10ns execution time, then the process having 5ns executes first.
The basic examples of Priority Queue
- Routing
- Operating System Scheduler
- Dijkstra's Shortest Path Algorithm
- Huffman Coding
C program to implement Priority Queue using array
In the given C program, we have asked the user for operations like create, insert, delete, check priority, and display. As indicated by the choice entered by the user, access its respective function using the switch statement.
In the function insert_element(), initially check if the queue is full. If it is, then print the result as "Queue Overflow". If not, take the number to be inserted as input and insert a new element into the priority queue.
In the function delete_element(), first check if the queue is empty. If it is, then print the result as "Queue Underflow". Otherwise, remove the element and the priority from the front of the queue.
In the function display_priorityqueue(), using for loop print all the elements of the queue, starting from front to rear.
The function check_priority() is used to check the priority and place element.
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void create_queue();
void insert_element(int);
void delete_element(int);
void check_priority(int);
void display_priorityqueue();
int pqueue[MAX];
int front, rear;
void main()
{
int n, choice;
printf("\nEnter 1 to insert element by priority ");
printf("\nEnter 2 to delete element by priority ");
printf("\nEnter 3 to display priority queue ");
printf("\nEnter 4 to exit");
create_queue();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert : ");
scanf("%d",&n);
insert_element(n);
break;
case 2:
printf("\nEnter element to delete : ");
scanf("%d",&n);
delete_element(n);
break;
case 3:
display_priorityqueue();
break;
case 4:
exit(0);
default:
printf("\n Please enter valid choice");
}
}
}
void create_queue()
{
front = rear = -1;
}
void insert_element(int data)
{
if (rear >= MAX - 1)
{
printf("\nQUEUE OVERFLOW");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pqueue[rear] = data;
return;
}
else
check_priority(data);
rear++;
}
void check_priority(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pqueue[i])
{
for (j = rear + 1; j > i; j--)
{
pqueue[j] = pqueue[j - 1];
}
pqueue[i] = data;
return;
}
}
pqueue[i] = data;
}
void delete_element(int data)
{
int i;
if ((front==-1) && (rear==-1))
{
printf("\nEmpty Queue");
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pqueue[i])
{
for (; i < rear; i++)
{
pqueue[i] = pqueue[i + 1];
}
pqueue[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d element not found in queue", data);
}
void display_priorityqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nEmpty Queue ");
return;
}
for (; front <= rear; front++)
{
printf(" %d ", pqueue[front]);
}
front = 0;
}
Output of the above code:
Enter 1 to insert element by priority
Enter 2 to delete element by priority
Enter 3 to display priority queue
Enter 4 to exit
Enter your choice : 1
Enter element to insert : 22
Enter your choice : 1
Enter element to insert : 90
Enter your choice : 1
Enter element to insert : 87
Enter your choice : 3
90 87 22
Enter your choice : 2
Enter element to delete : 87
Enter your choice : 3
90 22
Enter your choice : 4
...Program finished with exit code 0
Related Articles
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative