PriorityQueue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAX_SIZE 100
  4. typedef enum { INT_TYPE, FLOAT_TYPE, DOUBLE_TYPE } ElementType;
  5. typedef struct {
  6. ElementType type;
  7. union {
  8. int intValue;
  9. float floatValue;
  10. double doubleValue;
  11. } data;
  12. } Item;
  13. typedef struct {
  14. Item items[MAX_SIZE];
  15. int size;
  16. } PriorityQueue;
  17. PriorityQueue createPriorityQueue() {
  18. PriorityQueue pq;
  19. pq.size = 0;
  20. return pq;
  21. }
  22. bool isLess(Item item1, Item item2) {
  23. if (item1.type != item2.type) {
  24. fprintf(stderr, "错误:无法比较不同类型的元素。\n");
  25. return false;
  26. }
  27. switch (item1.type) {
  28. case INT_TYPE:
  29. return item1.data.intValue < item2.data.intValue;
  30. case FLOAT_TYPE:
  31. return item1.data.floatValue < item2.data.floatValue;
  32. case DOUBLE_TYPE:
  33. return item1.data.doubleValue < item2.data.doubleValue;
  34. default:
  35. fprintf(stderr, "错误:未知的元素类型。\n");
  36. return false;
  37. }
  38. }
  39. void swap(Item* item1, Item* item2) {
  40. Item temp = *item1;
  41. *item1 = *item2;
  42. *item2 = temp;
  43. }
  44. void push(PriorityQueue* pq, Item newItem) {
  45. if (pq->size >= MAX_SIZE) {
  46. fprintf(stderr, "错误:优先队列已满,无法插入元素。\n");
  47. return;
  48. }
  49. int childIndex = pq->size;
  50. pq->items[childIndex] = newItem;
  51. pq->size++;
  52. int parentIndex = (childIndex - 1) / 2;
  53. while (childIndex > 0 && isLess(pq->items[childIndex], pq->items[parentIndex])) {
  54. swap(&pq->items[childIndex], &pq->items[parentIndex]);
  55. childIndex = parentIndex;
  56. parentIndex = (childIndex - 1) / 2;
  57. }
  58. }
  59. Item pop(PriorityQueue* pq) {
  60. if (pq->size <= 0) {
  61. fprintf(stderr, "错误:优先队列为空,无法弹出元素。\n");
  62. Item emptyItem;
  63. emptyItem.type = INT_TYPE; // 返回一个空元素
  64. emptyItem.data.intValue = 0;
  65. return emptyItem;
  66. }
  67. Item minItem = pq->items[0];
  68. pq->size--;
  69. pq->items[0] = pq->items[pq->size];
  70. int parentIndex = 0;
  71. while (true) {
  72. int leftChildIndex = parentIndex * 2 + 1;
  73. int rightChildIndex = parentIndex * 2 + 2;
  74. int smallestIndex = parentIndex;
  75. if (leftChildIndex < pq->size && isLess(pq->items[leftChildIndex], pq->items[smallestIndex])) {
  76. smallestIndex = leftChildIndex;
  77. }
  78. if (rightChildIndex < pq->size && isLess(pq->items[rightChildIndex], pq->items[smallestIndex])) {
  79. smallestIndex = rightChildIndex;
  80. }
  81. if (smallestIndex == parentIndex) {
  82. break; // 已经是最小堆,无需继续下沉
  83. }
  84. swap(&pq->items[parentIndex], &pq->items[smallestIndex]);
  85. parentIndex = smallestIndex;
  86. }
  87. return minItem;
  88. }
  89. int main() {
  90. PriorityQueue pq = createPriorityQueue();
  91. // 向优先队列中插入一些元素
  92. Item item1, item2, item3;
  93. item1.type = INT_TYPE;
  94. item1.data.intValue = 5;
  95. item2.type = INT_TYPE;
  96. item2.data.intValue = 2;
  97. item3.type = INT_TYPE;
  98. item3.data.intValue = 10;
  99. push(&pq, item1);
  100. push(&pq, item2);
  101. push(&pq, item3);
  102. // 从优先队列中弹出元素
  103. Item minItem = pop(&pq);
  104. printf("弹出的最小元素为:%d\n", minItem.data.intValue);
  105. return 0;
  106. }
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。