typedef struct EdgeNode{
int adjvex;
int weight;
EdgeNode *next;
};
typedef struct VerTexNode {
int in;//入度
VertexType data;
EdgeNode *next;
}VerTexNode,AdjList[MAXVEX];
typedef struct {
AdjList adjlist;
int numEdges, numVertexes;
}graphadjlist,*GraphAdjList;
bool TopologicalSort(GraphAdjList G) {
EdgeNode *e;
int i, k, gettop;
int top = 0;
int count = 0;
int *stack;
stack = (int*)malloc((G->numVertexes)*sizeof(int));
for (i = 0; i < G->numVertexes; i++) {
if (G->adjlist[i].in == 0) {
stack[++top] = i;
}
}
while (top != 0) {
gettop = stack[top--];
cout << G->adjlist[gettop].data << "->";
count++;
for (e = G->adjlist[gettop].next; e; e = e->next) {
k = e->adjvex;
if (G->adjlist[k].in != 0)
stack[++top] = k;
}
}
if (count < G->numVertexes)
return 0;
else return 1;
}
图——AOV拓扑排序
最新推荐文章于 2022-06-23 12:22:40 发布