数据结构--拓扑排序

// TopologicalSort.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui------
-------2015-2-15--------------------*/

//拓扑排序

#include "stdafx.h"
#include "graph.h"
#include 

int indegree[MAX_VERTEX_NUM];

void FindInDegree(ALGraph G, int* indegree)//求各顶点的入度
{
    int i,j;
    ArcNode *p;
    for(i=0; iadjvex;
            indegree[j]++;
            p = p->nextarc;
        }
    }
}

int TopologicalSort(ALGraph G)//拓扑排序
{
    FindInDegree(G,indegree);
    stack S;
    int i,j;
    for(i=0; i";
        ++count;
        for(p=G.vertices[j].firstarc; p!=NULL; p=p->nextarc)
        {
            j = p->adjvex;    //对j号顶点的每个邻接点的入度减1
            if(!(--indegree[j]))//若入度为0,则进栈
                S.push(j);
        }
    }
    if(count

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