|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <string.h> |
| 5 | +#include <time.h> |
| 6 | + |
| 7 | +struct vertex; |
| 8 | +struct vertex_adjs { |
| 9 | + struct vertex *v; |
| 10 | + struct vertex_adjs *next; |
| 11 | +}; |
| 12 | + |
| 13 | +struct vertex { |
| 14 | + int data; |
| 15 | + struct vertex_adjs *adj; |
| 16 | +}; |
| 17 | + |
| 18 | +#define MAX_GRAPH_VERTEX (1 << 8) |
| 19 | +struct graph { |
| 20 | + struct vertex *vxs[MAX_GRAPH_VERTEX]; |
| 21 | +}; |
| 22 | + |
| 23 | +void init_graph(struct graph *graph) |
| 24 | +{ |
| 25 | + int i; |
| 26 | + |
| 27 | + for (i = 0; i < MAX_GRAPH_VERTEX; i++) |
| 28 | + graph->vxs[i] = NULL; |
| 29 | +} |
| 30 | + |
| 31 | +struct vertex *create_vertex(int data) |
| 32 | +{ |
| 33 | + struct vertex *v; |
| 34 | + |
| 35 | + v = malloc(sizeof(struct vertex)); |
| 36 | + |
| 37 | + if (v) { |
| 38 | + v->data = data; |
| 39 | + v->adj = NULL; |
| 40 | + } |
| 41 | + |
| 42 | + return v; |
| 43 | +} |
| 44 | + |
| 45 | +struct vertex_adjs *create_vertex_adj(struct vertex *v) |
| 46 | +{ |
| 47 | + struct vertex_adjs *v_adj; |
| 48 | + |
| 49 | + v_adj = malloc(sizeof(struct vertex_adjs)); |
| 50 | + |
| 51 | + if (!v_adj) |
| 52 | + return NULL; |
| 53 | + |
| 54 | + v_adj->v = v; |
| 55 | + v_adj->next = NULL; |
| 56 | + return v_adj; |
| 57 | +} |
| 58 | + |
| 59 | +void insert_adj(struct vertex *v, struct vertex *adj) |
| 60 | +{ |
| 61 | + struct vertex_adjs **v_adj; |
| 62 | + |
| 63 | + v_adj = &v->adj; |
| 64 | + |
| 65 | + while (*v_adj) |
| 66 | + v_adj = &(*v_adj)->next; |
| 67 | + |
| 68 | + *v_adj = create_vertex_adj(adj); |
| 69 | +} |
| 70 | + |
| 71 | +void dump_raw(struct graph *graph) |
| 72 | +{ |
| 73 | + int i; |
| 74 | + |
| 75 | + for (i = 0; i < MAX_GRAPH_VERTEX; i++) { |
| 76 | + struct vertex *v = graph->vxs[i]; |
| 77 | + struct vertex_adjs *adj; |
| 78 | + if (v == NULL) |
| 79 | + continue; |
| 80 | + |
| 81 | + printf("Vertex[%02d]: %8d ->", i, v->data); |
| 82 | + |
| 83 | + adj = v->adj; |
| 84 | + while (adj) { |
| 85 | + printf(" %8d,", adj->v->data); |
| 86 | + adj = adj->next; |
| 87 | + } |
| 88 | + printf("\n"); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/* |
| 93 | + 1 ----- 2 ----- 3 |
| 94 | + | / | / |
| 95 | + | / | / |
| 96 | + | / | / |
| 97 | + | / | / |
| 98 | + | / | / |
| 99 | + 4 ----- 5 |
| 100 | +*/ |
| 101 | +void fake_a_graph(struct graph *graph) |
| 102 | +{ |
| 103 | + int i; |
| 104 | + |
| 105 | + init_graph(graph); |
| 106 | + |
| 107 | + for (i = 0; i < 5; i++) |
| 108 | + graph->vxs[i] = create_vertex(i+1); |
| 109 | + |
| 110 | + /* connect 1 -> 2, 1 -> 4 */ |
| 111 | + insert_adj(graph->vxs[0], graph->vxs[1]); |
| 112 | + insert_adj(graph->vxs[0], graph->vxs[3]); |
| 113 | + /* connect 2 -> 1, 2 -> 3, 2 -> 5, 2 -> 4 */ |
| 114 | + insert_adj(graph->vxs[1], graph->vxs[0]); |
| 115 | + insert_adj(graph->vxs[1], graph->vxs[2]); |
| 116 | + insert_adj(graph->vxs[1], graph->vxs[4]); |
| 117 | + insert_adj(graph->vxs[1], graph->vxs[3]); |
| 118 | + /* connect 3 -> 2, 3 -> 5 */ |
| 119 | + insert_adj(graph->vxs[2], graph->vxs[1]); |
| 120 | + insert_adj(graph->vxs[2], graph->vxs[4]); |
| 121 | + /* connect 4 -> 1, 4 -> 2, 4 -> 5 */ |
| 122 | + insert_adj(graph->vxs[3], graph->vxs[0]); |
| 123 | + insert_adj(graph->vxs[3], graph->vxs[1]); |
| 124 | + insert_adj(graph->vxs[3], graph->vxs[4]); |
| 125 | + /* connect 5 -> 4, 5 -> 2, 5 -> 3 */ |
| 126 | + insert_adj(graph->vxs[4], graph->vxs[3]); |
| 127 | + insert_adj(graph->vxs[4], graph->vxs[1]); |
| 128 | + insert_adj(graph->vxs[4], graph->vxs[3]); |
| 129 | +} |
| 130 | + |
| 131 | +int main() |
| 132 | +{ |
| 133 | + struct graph g; |
| 134 | + |
| 135 | + fake_a_graph(&g); |
| 136 | + dump_raw(&g); |
| 137 | + return 0; |
| 138 | +} |
0 commit comments