#include
#include
typedef struct node *link;
typedef struct edge Edge;
typedef struct graph *Graph;
struct node { int V; link next; };
struct edge { int w; int v; };
struct graph { int V; int E; link *adj; };
link NEW(int v, link next)
{
link x = malloc(sizeof *x);
x->V = v; x->next = next;
return x;
}
Edge EDGE(int v, int w)
{
Edge *x = malloc(sizeof *x);
x->v = v; x->w = w;
return *x;
}
Graph GRAPHinit(int V)
{
int v;
Graph G = malloc(sizeof *G);
G->V = V; G->E = 0;
G->adj = malloc(V*sizeof(link));
for (v = 0; v < V; v++) G->adj[v] = NULL;
return G;
}
void GRAPHinsertE(Graph G, Edge e)
{
int v = e.v, w = e.w;
G->adj[v] = NEW(w, G->adj[v]);
G->adj[w] = NEW(v, G->adj[w]);
G->E++;
}
int GRAPHedges(Edge a[], Graph G)
{
int v, E = 0; link t;
for (v = 0; v < G->V; v++)
{
printf("%d: ", v);
for(t = G->adj[v]; t != NULL; t = t->next)
{
a[E] = EDGE(v, t->V);
printf("->[%d]", a[E].w);
E++;
}
printf("\n");
}
return E;
}
int main()
{
Graph G = NULL;
Edge e[] = {{3, 5}, {1, 3}, {6, 1}, {7, 8}, {3, 4}};
Edge a[100] = {0};
int i;
G = GRAPHinit(10);
for(i = 0; i < sizeof(e)/sizeof(e[0]); i++)
{
GRAPHinsertE(G, e[i]);
}
GRAPHedges(a, G);
}
阅读(2731) | 评论(1) | 转发(0) |