2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
72
Task/Priority-queue/C/priority-queue.c
Normal file
72
Task/Priority-queue/C/priority-queue.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int priority;
|
||||
char *data;
|
||||
} node_t;
|
||||
|
||||
typedef struct {
|
||||
node_t *nodes;
|
||||
int len;
|
||||
int size;
|
||||
} heap_t;
|
||||
|
||||
void push (heap_t *h, int priority, char *data) {
|
||||
if (h->len + 1 >= h->size) {
|
||||
h->size = h->size ? h->size * 2 : 4;
|
||||
h->nodes = (node_t *)realloc(h->nodes, h->size * sizeof (node_t));
|
||||
}
|
||||
int i = h->len + 1;
|
||||
int j = i / 2;
|
||||
while (i > 1 && h->nodes[j].priority > priority) {
|
||||
h->nodes[i] = h->nodes[j];
|
||||
i = j;
|
||||
j = j / 2;
|
||||
}
|
||||
h->nodes[i].priority = priority;
|
||||
h->nodes[i].data = data;
|
||||
h->len++;
|
||||
}
|
||||
|
||||
char *pop (heap_t *h) {
|
||||
int i, j, k;
|
||||
if (!h->len) {
|
||||
return NULL;
|
||||
}
|
||||
char *data = h->nodes[1].data;
|
||||
h->nodes[1] = h->nodes[h->len];
|
||||
h->len--;
|
||||
i = 1;
|
||||
while (1) {
|
||||
k = i;
|
||||
j = 2 * i;
|
||||
if (j <= h->len && h->nodes[j].priority < h->nodes[k].priority) {
|
||||
k = j;
|
||||
}
|
||||
if (j + 1 <= h->len && h->nodes[j + 1].priority < h->nodes[k].priority) {
|
||||
k = j + 1;
|
||||
}
|
||||
if (k == i) {
|
||||
break;
|
||||
}
|
||||
h->nodes[i] = h->nodes[k];
|
||||
i = k;
|
||||
}
|
||||
h->nodes[i] = h->nodes[h->len + 1];
|
||||
return data;
|
||||
}
|
||||
|
||||
int main () {
|
||||
heap_t *h = (heap_t *)calloc(1, sizeof (heap_t));
|
||||
push(h, 3, "Clear drains");
|
||||
push(h, 4, "Feed cat");
|
||||
push(h, 5, "Make tea");
|
||||
push(h, 1, "Solve RC tasks");
|
||||
push(h, 2, "Tax return");
|
||||
int i;
|
||||
for (i = 0; i < 5; i++) {
|
||||
printf("%s\n", pop(h));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue