below. 1. /* 2. * C Program to Implement Priority Queue to Add and - TopicsExpress



          

below. 1. /* 2. * C Program to Implement Priority Queue to Add and Delete Elements 3. */ 4. #include 5. #include 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(" 1 - Insert an element into queue"); 23. printf(" 2 - Delete an element from queue"); 24. printf(" 3 - Display queue elements"); 25. printf(" 4 - Exit"); 26. 27. create(); 28. 29. while (1) 30. { 31. printf(" Enter your choice : "); 32. scanf("%d", &ch); 33. 34. switch (ch) 35. { 36. case 1: 37. printf(" Enter value to be inserted : "); 38. scanf("%d",&n); 39. insert_by_priority (n); 40. break; 41. case 2: 42. printf(" Enter 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(" Choice 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(" Queue 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 = 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(" Queue is empty no elements to delete"); 111. return; 112. } 113. 114. for (i = 0; i
Posted on: Tue, 17 Sep 2013 16:25:50 +0000

Trending Topics



Recently Viewed Topics




© 2015