|
楼主 |
发表于 2014-7-30 18:18:49
|
显示全部楼层
全部的代码 如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct EdgeNode
{
int data;
struct EdgeNode *next;
}EdgeNode,*Edge;
typedef struct VertexNode
{
int in;
char data;
EdgeNode *first;
}VertexNode;
typedef VertexNode Graph[14];
void CreatGraph(Graph *G)
{
G = (Graph *)malloc(14 * sizeof(VertexNode));
G[0]->data = 'a';
G[1]->data = 'b';
G[2]->data = 'c';
G[3]->data = 'd';
G[4]->data = 'e';
G[5]->data = 'f';
G[6]->data = 'g';
G[7]->data = 'h';
G[8]->data = 'i';
G[9]->data = 'j';
G[10]->data = 'k';
G[11]->data = 'l';
G[12]->data = 'm';
G[13]->data = 'n';
G[14]->data = 'o';
G[0]->in = 0;
G[1]->in = 2;
G[2]->in = 2;
G[3]->in = 2;
G[4]->in = 1;
G[5]->in = 1;
G[6]->in = 3;
G[7]->in = 1;
G[8]->in = 1;
G[9]->in = 2;
G[10]->in = 1;
G[11]->in = 1;
G[12]->in = 0;
G[13]->in = 1;
G[14]->in = 1;
int i;
for (i = 0; i < 15; i++)
{
G[i]->first = NULL;
}
Edge p, q;
p = (Edge)malloc(sizeof(EdgeNode));
p->data = 1;
p->next = NULL;
q = p;
G[0]->first = p;
p = (Edge)malloc(sizeof(EdgeNode));
p->data = 2;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 3;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 5;
p->next = NULL;
q = p;
G[2]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 6;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 7;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 8;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 7;
p->next = NULL;
q = p;
G[3]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 6;
p->next = NULL;
q = p;
G[9]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 10;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 11;
p->next = NULL;
q = p;
G[10]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 13;
p->next = NULL;
q = p;
G[12]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 3;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 14;
p->next = NULL;
q = p;
G[13]->first = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 3;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 2;
p->next = NULL;
q->next = p;
q = p;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
p->data = 4;
p->next = NULL;
q = p;
G[14]->first = p;
}
void Topo(Graph *G)
{
Graph temp;
int i;
for (i = 0; i < 15; i++)
{
temp[i].in = G[i]->in;
temp[i].data = G[i]->data;
temp[i].first = G[i]->first;
}
int j,temp_in;
EdgeNode *p;
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15;j++)
{
if (temp[j].in == 0)
{
printf("->%c", temp[j].data);
p = G[j]->first;
while (p)
{
temp_in = p->data;
temp[temp_in].in--;
p = p->next;
}
}
}
}
}
int main(void)
{
Graph G;
CreatGraph(&G);
Topo(&G);
return 0;
} |
|