#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define MVNum 100 typedef int Status; typedef char VerTexType; typedef char OtherInfo;
typedef struct StackNode { int data; struct StackNode* next; }StackNode, * StackList;
StackList Pop(StackList S, int* e) { StackList p; p = S; if (!p) return ERROR; *e = p->data; S = S->next; free(p); return S; }
StackList Push(StackList S, int e) { StackList p; p = (StackNode*)malloc(sizeof(StackNode)); p->data = e; p->next = S; S = p; return S; }
typedef struct ArcNode { int adjvex; struct ArcNode* nextarc; OtherInfo info; }ArcNode;
typedef struct VNode { VerTexType data; ArcNode* firstarc; }VNode, AdjList[MVNum];
typedef struct { AdjList vertices; int vexnum, arcnum; }ALGraph;
int indegree[100] = { 0 };
void TopoSort(ALGraph G, int* topo) { StackList S = NULL; ArcNode* p; for (int i = 0; i < G.vexnum; i++) { if (!indegree[i]) { S = Push(S, i); } } int m = 0; int index = 0; while (S) { S = Pop(S, &index); topo[m] = index; m++; while (p != NULL) { int k = p->adjvex; indegree[k]--; if (indegree[k] == 0) { S = Push(S, k); } p = p->nextarc; } } topo[m] = -1; if (m < G.vexnum) { } else { } }
|