数据结构-拓扑排序

#include
#define MAX 100
typedef struct ArcNode{
    int adjvex;
    struct ArcNode *nextarc;
}ArcNode;

typedef struct VNode{
    char vertex;
    ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAX];
typedef struct ALGraph{
    AdjList adjlist;
    int vexnum;
    int arcnum;
}ALGraph;

void FindInDegree(ALGraph G,int *indegree);
void TopologicalSort(ALGraph G,int n)
{
    int count,i,k,indegree[n];
    int top;    
    ArcNode *p;
    FindInDegree(G,indegree);
    top = 0;
    for(i=0;i         if(indegree[i]==0){
            indegree[i] = top;
            top = i + 1;
        }
    }
    count = count = 0;
    while(top!=0){
        i = top - 1;
        top = indegree[i];
        printf("%c",G.adjlist[i].vertex);
        count++;
        for(p=G.adjlist[i].firstarc;p;p=p->nextarc){
            k = p->adjvex;
            if((--indegree[k]==0)){
                indegree[k] = top;
                top = k + 1;
            }
        }
    }
    if(count         printf("此有向图有回路\n");
    else
        printf("输出序列为一个拓扑有序序列.\n"); 
}

void FindInDegree(ALGraph G,int *indegree)
{
    int i;
    ArcNode *p;
    for(i=0;i         indegree[i] = 0;
    for(i=0;i         p = G.adjlist[i].firstarc;
        while(p){
            indegree[p->adjvex]++;
            p = p->nextarc;
        }
    }
}

int main()
{
    return 0;

你可能感兴趣的:(数据结构,算法)