Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
71
Task/Priority-queue/C/priority-queue-1.c
Normal file
71
Task/Priority-queue/C/priority-queue-1.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#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 (i!=h->len+1) {
|
||||
k = h->len+1;
|
||||
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;
|
||||
}
|
||||
h->nodes[i] = h->nodes[k];
|
||||
i = k;
|
||||
}
|
||||
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;
|
||||
}
|
||||
18
Task/Priority-queue/C/priority-queue-2.c
Normal file
18
Task/Priority-queue/C/priority-queue-2.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
typedef struct _pq_node_t {
|
||||
long int key;
|
||||
struct _pq_node_t *next, *down;
|
||||
} pq_node_t, *heap_t;
|
||||
|
||||
extern heap_t heap_merge(heap_t, heap_t);
|
||||
extern heap_t heap_pop(heap_t);
|
||||
|
||||
#define NEW_PQ_ELE(p, k) \
|
||||
do { \
|
||||
(p) = (typeof(p)) malloc(sizeof(*p)); \
|
||||
((pq_node_t *) (p))->next = ((pq_node_t *) (p))->down = NULL; \
|
||||
((pq_node_t *) (p))->key = (k); \
|
||||
} while (0)
|
||||
|
||||
#define HEAP_PUSH(p, k, h) \
|
||||
NEW_PQ_ELE(p, k); \
|
||||
*(h) = heap_merge(((pq_node_t *) (p)), *(h))
|
||||
44
Task/Priority-queue/C/priority-queue-3.c
Normal file
44
Task/Priority-queue/C/priority-queue-3.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdlib.h>
|
||||
#include "pairheap.h"
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Pairing heap implementation
|
||||
* --------------------------------------------------------------------------- */
|
||||
|
||||
static heap_t add_child(heap_t h, heap_t g) {
|
||||
if (h->down != NULL)
|
||||
g->next = h->down;
|
||||
h->down = g;
|
||||
}
|
||||
|
||||
heap_t heap_merge(heap_t a, heap_t b) {
|
||||
if (a == NULL) return b;
|
||||
if (b == NULL) return a;
|
||||
if (a->key < b->key) {
|
||||
add_child(a, b);
|
||||
return a;
|
||||
} else {
|
||||
add_child(b, a);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
/* NOTE: caller should have pointer to top of heap, since otherwise it won't
|
||||
* be reclaimed. (we do not free the top.)
|
||||
*/
|
||||
heap_t two_pass_merge(heap_t h) {
|
||||
if (h == NULL || h->next == NULL)
|
||||
return h;
|
||||
else {
|
||||
pq_node_t
|
||||
*a = h,
|
||||
*b = h->next,
|
||||
*rest = b->next;
|
||||
a->next = b->next = NULL;
|
||||
return heap_merge(heap_merge(a, b), two_pass_merge(rest));
|
||||
}
|
||||
}
|
||||
|
||||
heap_t heap_pop(heap_t h) {
|
||||
return two_pass_merge(h->down);
|
||||
}
|
||||
36
Task/Priority-queue/C/priority-queue-4.c
Normal file
36
Task/Priority-queue/C/priority-queue-4.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "pairheap.h"
|
||||
|
||||
struct task {
|
||||
pq_node_t hd;
|
||||
char task[40];
|
||||
};
|
||||
|
||||
void main() {
|
||||
heap_t heap = NULL;
|
||||
struct task *new;
|
||||
|
||||
HEAP_PUSH(new, 3, &heap);
|
||||
strcpy(new->task, "Clear drains.");
|
||||
|
||||
HEAP_PUSH(new, 4, &heap);
|
||||
strcpy(new->task, "Feed cat.");
|
||||
|
||||
HEAP_PUSH(new, 5, &heap);
|
||||
strcpy(new->task, "Make tea.");
|
||||
|
||||
HEAP_PUSH(new, 1, &heap);
|
||||
strcpy(new->task, "Solve RC tasks.");
|
||||
|
||||
HEAP_PUSH(new, 2, &heap);
|
||||
strcpy(new->task, "Tax return.");
|
||||
|
||||
while (heap != NULL) {
|
||||
struct task *top = (struct task *) heap;
|
||||
printf("%s\n", top->task);
|
||||
heap = heap_pop(heap);
|
||||
free(top);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue