C Program to Implement Priority Queue Operations

This is a C Program to implement priority queue to add and delete elements.

Problem Description

This C program implements the operations of the priority queue.

Problem Solution

1. Add the elements into the queue according to the order (ascending or descending).
2. Delete the elements.

Program/Source Code

Here is source code of the C Program to implement priority queue to add and delete elements. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Implement Priority Queue to Add and Delete Elements
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX 5
  8.  
  9. void insert_by_priority(int);
  10. void delete_by_priority(int);
  11. void create();
  12. void check(int);
  13. void display_pqueue();
  14.  
  15. int pri_que[MAX];
  16. int front, rear;
  17.  
  18. void main()
  19. {
  20.     int n, ch;
  21.  
  22.     printf("\n1 - Insert an element into queue");
  23.     printf("\n2 - Delete an element from queue");
  24.     printf("\n3 - Display queue elements");
  25.     printf("\n4 - Exit");
  26.  
  27.     create();
  28.  
  29.     while (1)
  30.     {
  31.         printf("\nEnter your choice : ");    
  32.         scanf("%d", &ch);
  33.  
  34.         switch (ch)
  35.         {
  36.         case 1: 
  37.             printf("\nEnter value to be inserted : ");
  38.             scanf("%d",&n);
  39.             insert_by_priority(n);
  40.             break;
  41.         case 2:
  42.             printf("\nEnter value to delete : ");
  43.             scanf("%d",&n);
  44.             delete_by_priority(n);
  45.             break;
  46.         case 3: 
  47.             display_pqueue();
  48.             break;
  49.         case 4: 
  50.             exit(0);
  51.         default: 
  52.             printf("\nChoice is incorrect, Enter a correct choice");
  53.         }
  54.     }
  55. }
  56.  
  57. /* Function to create an empty priority queue */
  58. void create()
  59. {
  60.     front = rear = -1;
  61. }
  62.  
  63. /* Function to insert value into priority queue */
  64. void insert_by_priority(int data)
  65. {
  66.     if (rear >= MAX - 1)
  67.     {
  68.         printf("\nQueue overflow no more elements can be inserted");
  69.         return;
  70.     }
  71.     if ((front == -1) && (rear == -1))
  72.     {
  73.         front++;
  74.         rear++;
  75.         pri_que[rear] = data;
  76.         return;
  77.     }    
  78.     else
  79.         check(data);
  80.     rear++;
  81. }
  82.  
  83. /* Function to check priority and place element */
  84. void check(int data)
  85. {
  86.     int i,j;
  87.  
  88.     for (i = 0; i <= rear; i++)
  89.     {
  90.         if (data >= pri_que[i])
  91.         {
  92.             for (j = rear + 1; j > i; j--)
  93.             {
  94.                 pri_que[j] = pri_que[j - 1];
  95.             }
  96.             pri_que[i] = data;
  97.             return;
  98.         }
  99.     }
  100.     pri_que[i] = data;
  101. }
  102.  
  103. /* Function to delete an element from queue */
  104. void delete_by_priority(int data)
  105. {
  106.     int i;
  107.  
  108.     if ((front==-1) && (rear==-1))
  109.     {
  110.         printf("\nQueue is empty no elements to delete");
  111.         return;
  112.     }
  113.  
  114.     for (i = 0; i <= rear; i++)
  115.     {
  116.         if (data == pri_que[i])
  117.         {
  118.             for (; i < rear; i++)
  119.             {
  120.                 pri_que[i] = pri_que[i + 1];
  121.             }
  122.  
  123.         pri_que[i] = -99;
  124.         rear--;
  125.  
  126.         if (rear == -1) 
  127.             front = -1;
  128.         return;
  129.         }
  130.     }
  131.     printf("\n%d not found in queue to delete", data);
  132. }
  133.  
  134. /* Function to display queue elements */
  135. void display_pqueue()
  136. {
  137.     if ((front == -1) && (rear == -1))
  138.     {
  139.         printf("\nQueue is empty");
  140.         return;
  141.     }
  142.  
  143.     for (; front <= rear; front++)
  144.     {
  145.         printf(" %d ", pri_que[front]);
  146.     }
  147.  
  148.     front = 0;
  149. }
Runtime Test Cases
1 - Insert an element into queue
2 - Delete an element from queue
3 - Display queue elements
4 - Exit
Enter your choice : 1
 
Enter value to be inserted : 20
 
Enter your choice : 1
 
Enter value to be inserted : 45
 
Enter your choice : 1
 
Enter value to be inserted : 89
 
Enter your choice : 3
 89  45  20 
Enter your choice : 1
 
Enter value to be inserted : 56
 
Enter your choice : 3
 89  56  45  20 
Enter your choice : 2
 
Enter value to delete : 45
 
Enter your choice : 3
 89  56  20 
Enter your choice : 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Stacks & Queues, go to C Programming Examples on Stacks & Queues. If you wish to look at programming examples on all topics of C, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.