算法思路参照严蔚敏《数据结构》 /* 图用邻接表存储 拓扑排序 图中弧的指向都是向右的 C4----------------- ------>C5 /// /// / C2 / / /// /// / C1----------------------C3----------------C7 / / /// /// C12 C8 /// /// /// / / / C9---------->C10 C6 / /// / / /// / C11 */ #include <iostream> #include <stack> using namespace std; const int maxVertexNum = 20; typedef struct ArcNode { int adjvex; //该弧所指向的顶点在Adjust即ALGraph.vertices中的位置 ArcNode* nextarc; //指向下一条弧的指针 int info; //弧信息 }ArcNode, *PArcNode; typedef struct VNode { char data; //顶点信息 ArcNode* firstarc; //指向第一条依附于该节点的弧的指针 }VNode, Adjust[maxVertexNum]; typedef struct ALGraph { Adjust vertices; //顶点数组 int vexnum; //顶点数 int arcnum; //弧数 int kind; //1(无向图),2(有向图) }ALGraph; void BuildGraph(ALGraph& G) { G.vexnum = G.arcnum = 0; G.kind = 2; ArcNode* pFirst = NULL; ArcNode* pCurrent = NULL; ArcNode* pArc = NULL; //C1 pArc = new ArcNode; pArc->adjvex = 1; pArc->info = 1; G.arcnum++; pFirst = pCurrent = pArc; pArc = new ArcNode; pArc->adjvex = 2; pArc->info = 1; pCurrent->nextarc = pArc; pCurrent = pArc; G.arcnum++; pArc = new ArcNode; pArc->adjvex = 3; pArc->info = 1; pCurrent->nextarc = pArc; pCurrent = pArc; G.arcnum++; pArc = new ArcNode; pArc->adjvex = 11; pArc->info = 1; pCurrent->nextarc = pArc; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '1'; G.vertices[G.vexnum++].firstarc = pFirst; //C2 pArc = new ArcNode; pArc->adjvex = 2; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '2'; G.vertices[G.vexnum++].firstarc = pArc; //C3 pArc = new ArcNode; pArc->adjvex = 4; pArc->info = 1; G.arcnum++; pFirst = pCurrent = pArc; pArc = new ArcNode; pArc->adjvex = 6; pArc->info = 1; pCurrent->nextarc = pArc; pCurrent = pArc; G.arcnum++; pArc = new ArcNode; pArc->adjvex = 7; pArc->info = 1; pCurrent->nextarc = pArc; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '3'; G.vertices[G.vexnum++].firstarc = pFirst; //C4 pArc = new ArcNode; pArc->adjvex = 4; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '4'; G.vertices[G.vexnum++].firstarc = pArc; //C5 pArc = new ArcNode; pArc->adjvex = 6; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '5'; G.vertices[G.vexnum++].firstarc = pArc; //C6 pArc = new ArcNode; pArc->adjvex = 7; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '6'; G.vertices[G.vexnum++].firstarc = pArc; //C7 G.vertices[G.vexnum].data = '7'; G.vertices[G.vexnum++].firstarc = NULL; //C8 G.vertices[G.vexnum].data = '8'; G.vertices[G.vexnum++].firstarc = NULL; //C9 pArc = new ArcNode; pArc->adjvex = 9; pArc->info = 1; G.arcnum++; pFirst = pCurrent = pArc; pArc = new ArcNode; pArc->adjvex = 10; pArc->info = 1; pCurrent->nextarc = pArc; pCurrent = pArc; G.arcnum++; pArc = new ArcNode; pArc->adjvex = 11; pArc->info = 1; pCurrent->nextarc = pArc; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = '9'; G.vertices[G.vexnum++].firstarc = pFirst; //C10 pArc = new ArcNode; pArc->adjvex = 11; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = 'a'; G.vertices[G.vexnum++].firstarc = pArc; //C11 pArc = new ArcNode; pArc->adjvex = 5; pArc->info = 1; pArc->nextarc = NULL; G.arcnum++; G.vertices[G.vexnum].data = 'b'; G.vertices[G.vexnum++].firstarc = pArc; //C12 G.vertices[G.vexnum].data = 'c'; G.vertices[G.vexnum++].firstarc = NULL; } void DestroyGraph(ALGraph G) { PArcNode pCurrent = NULL; PArcNode pNext = NULL; for(int i = 0; i < G.vexnum; i++) { for(pCurrent = G.vertices[i].firstarc; pCurrent; pCurrent = pNext) { pNext = pCurrent->nextarc; delete pCurrent; G.arcnum--; } } } bool TopologicalSort(const ALGraph& G) { int* indegree = new int[G.vexnum]; for(int i = 0; i < G.vexnum; i++) indegree[i] = 0; for(int i = 0; i < G.vexnum; i++) { for(PArcNode p = G.vertices[i].firstarc; p; p = p->nextarc) { indegree[p->adjvex]++; } } stack<int> S; for(int i = 0; i < G.vexnum; i++) { if(!indegree[i]) S.push(i); } int count = 0; while(!S.empty()) { int c = S.top(); S.pop(); cout << c+1 << "-->"; ++count; for(PArcNode p = G.vertices[c].firstarc; p; p = p->nextarc) { int k = p->adjvex; if(!(--indegree[k])) S.push(k); } } delete[] indegree; if(count < G.vexnum) return false; return true; } int main() { ALGraph myGraph; BuildGraph(myGraph); TopologicalSort(myGraph); DestroyGraph(myGraph); return 0; }