C语言实现优先队列

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 #define MinPQSize (10)
  6 #define MinData (-32767)
  7 
  8 struct heap
  9 {
 10     int capacity;
 11     int size;
 12     int *elements;
 13 };
 14 
 15 typedef struct heap *pri_queue;
 16 
 17 pri_queue pri_queue_init(int queue_size);  // 初始化一个优先队列
 18 void push_pri_queue(int val, pri_queue H); // 将元素放入优先队列
 19 int pop_pri_queue(pri_queue H);            // 将优先队列的元素取出
 20 void make_prique_empty(pri_queue H);       // 设置优先队列为空
 21 int find_que_min(pri_queue H);             // 在优先队列里搜索元素
 22 int is_queue_empty(pri_queue H);           // 判断优先队列是不是为空
 23 int is_queue_full(pri_queue H);            // 判断优先队列是不是满
 24 void destroy_queue(pri_queue H);           // 销毁优先队列
 25 void printf_pri_queue(pri_queue H);        // 直接输出整个优先队列的内容
 26 
 27 
 28 
 29 /* 测试优先队列 */
 30 int main(int argc, char **argv)
 31 {
 32     pri_queue H = pri_queue_init(50);
 33     int ar[] = {11, 2, 7, 8, 3, 4, 12, 13, 14, 1, 10, 15, 6, 9, 29, 11, 23, 22, 13, 14, 5, 28, 16, 26, 27, 17, 18, 12, 20, 21, 31, 30, 24, 25, 19};
 34     int i;
 35 
 36     for (i = 0; i < sizeof(ar) / sizeof(int); i++)
 37         push_pri_queue(ar[i], H);
 38 
 39     printf_pri_queue(H);
 40 
 41     printf("\n测试pop_pri_queue():\n");
 42     for (i = 0; i < sizeof(ar) / sizeof(int); i++)
 43     {
 44         printf("%d ", pop_pri_queue(H));
 45     }
 46     printf("\n\n");
 47 
 48     return 0;
 49 }
 50 
 51 
 52 // 初始化一个优先队列
 53 pri_queue pri_queue_init(int queue_size)
 54 {
 55     pri_queue H;
 56 
 57     if (queue_size < MinPQSize)
 58         exit(EXIT_FAILURE);
 59 
 60     H = (pri_queue)calloc(1, sizeof(struct heap));
 61     if (H == NULL)
 62         exit(EXIT_FAILURE);
 63 
 64     H->elements = (pri_queue)calloc(1, (queue_size + 1) * sizeof(int));
 65     if (H->elements == NULL)
 66         exit(EXIT_FAILURE);
 67 
 68     H->capacity = queue_size;
 69     H->size = 0;
 70     H->elements[0] = MinData;
 71 
 72     return H;
 73 }
 74 
 75 // 将元素放入优先队列
 76 void push_pri_queue(int val, pri_queue H)
 77 {
 78     int i;
 79 
 80     if (is_queue_full(H))
 81         return;
 82 
 83     for (i = ++H->size; H->elements[i / 2] > val; i /= 2)
 84         H->elements[i] = H->elements[i / 2];
 85 
 86     H->elements[i] = val;
 87 }
 88 
 89 // 将优先队列的元素取出
 90 int pop_pri_queue(pri_queue H)
 91 {
 92     int i, child;
 93     int min, last;
 94 
 95     if (is_queue_empty(H))
 96         return H->elements[0];
 97 
 98     min = H->elements[1];
 99     last = H->elements[H->size--];
100 
101     for (i = 1; i * 2 <= H->size; i = child)
102     {
103         child = i * 2;
104 
105         if (child != H->size && H->elements[child + 1] < H->elements[child])
106             child++;
107 
108         if (last > H->elements[child])
109             H->elements[i] = H->elements[child];
110         else
111             break;
112     }
113 
114     H->elements[i] = last;
115 
116     return min;
117 }
118 
119 // 设置优先队列为空
120 void make_prique_empty(pri_queue H)
121 {
122     H->size = 0;
123 }
124 
125 // 在优先队列里搜索元素
126 int find_que_min(pri_queue H)
127 {
128     if (!is_queue_empty(H))
129         return H->elements[1];
130 
131     printf("queue empty\n");
132     return H->elements[0];
133 }
134 
135 // 判断优先队列是不是为空
136 int is_queue_empty(pri_queue H)
137 {
138     return (H->size == 0);
139 }
140 
141 // 判断优先队列是不是满
142 int is_queue_full(pri_queue H)
143 {
144     return (H->size == H->capacity);
145 }
146 
147 // 销毁优先队列
148 void destroy_queue(pri_queue H)
149 {
150     free(H->elements);
151     H->elements = NULL;
152     free(H);
153     H = NULL;
154 }
155 
156 void printf_pri_queue(pri_queue H)
157 {
158     int i = 0;
159 
160     printf("\n直接输出优先队列的内容:\n");
161 
162     for (i = 1; i <= H->size; i++)
163         printf("%d ", H->elements[i]);
164 
165     printf("\n\n");
166 }

 

posted @ 2022-06-16 18:04  天外仙宗  阅读(41)  评论(0)    收藏  举报