严蔚敏教材伪代码如下:
Status TopologicalSort( ALGraph G)
{
//有向图G采用邻接表存储结构。
//若G无回路,则输出G的顶点的一个拓扑序列并返回OK,否则ERROR
FindInDegree( G, indegree); //对各顶点求入度
InitStack(S);
for ( int i = 0; i < G.vexnum; i++) //建立入读顶点栈S
if (!indegree[i])Push(S,i); //入度为0进栈
count = 0; //对输出顶点计数
while( !StackEmpty(S))
{
Pop(S, i); printf(i, G.vertices[i].data); ++count;
for ( p = G.vertices[i].firstarc; p; p = p ->nextarc)
{
k = p ->adjvex;
if ( !(--indegree[k])) Push(S,k); //若入度为0,进栈
}
}
if ( cout < G.vexnum ) return ERROR; //该有向图有回路,出错
else return OK:
}
汗……写的字好难看啊,此乃作者笔记,无关人等尽管忽略