图——AOV拓扑排序

来源:互联网 发布:淘宝优惠券推广拿佣金 编辑:程序博客网 时间:2024/05/17 01:11
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;}
0 0
原创粉丝点击