File: main.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (82 lines) | stat: -rw-r--r-- 1,619 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "fibheap.h"

typedef struct {
	char payload[32];
	fbhn_t fbhn;
} entry_t;

entry_t *new_entry(fbh_t *fbh, int pri, char *payload)
{
	entry_t *e = malloc(sizeof(entry_t));
	strcpy(e->payload, payload);
	fbh_insert(fbh, e, pri);
	return e;
}

void print_subtree(fbh_t *fbh, fbhn_t *root, int ind)
{
	fbhn_t *n = root;

	if (n == NULL)
		return;

	do {
		int i;
		entry_t *e = fbh_n2o(fbh, n);
		for(i = 0; i < ind; i++) fputc(' ', stdout);
		printf("%ld %s (parent=%ld) %s\n", n->pri, e->payload, n->parent != NULL ? n->parent->pri : -1, fbh->min == n ? "*":"");
		print_subtree(fbh, n->child, ind+1);
		n = n->right;
	} while(n != root);
}

void print_tree(fbh_t *fbh, const char *title)
{
	printf("%s:\n", title);
	print_subtree(fbh, fbh->min, 1);
}

int main()
{
	int r, n;
	fbh_t fbh;
	entry_t *e;
	const long pritab[] = {3, 6, 7, 5, 3, 5, 6, 2};

/*	printf("size, offs: %d %d\n", sizeof(fbhn_t), offsetof(entry_t, fbhn)); */

	r = fbh_init(&fbh, offsetof(entry_t, fbhn));
	assert(r == 0);

	e = fbh_min(&fbh);
	assert(e == NULL);

	for(n = 0; n < 8; n++) {
		char tmp[32];
		int pri = pritab[n];
		sprintf(tmp, "{%04d}", pri);
		printf("add: %d %s\n", pri, tmp);
		e = new_entry(&fbh, pri, tmp);
		assert(e != NULL);
	}

	print_tree(&fbh, "pre");

	e = fbh_min(&fbh);
	assert(e != NULL);
	printf("\nmin: %ld %s\n", e->fbhn.pri, e->payload);

	printf("\npop min:\n");
	while(fbh.num_nodes > 0) {
		e = fbh_pop_min(&fbh);
		assert(e != NULL);
		printf("-> %ld %s\n", e->fbhn.pri, e->payload);
	}

	return 0;
}