源代码
//拓扑排序
//2014-3-13
#include <iostream>
#include <stack>
using namespace std;
#define MAX_VERTEXS 100
class Graph
{
public:
typedef struct _ArcNode //顶点 结构体
{
int nkey;
_ArcNode* next;
_ArcNode():next(NULL){};
}ArcNode, *pArcNode;
typedef struct _AdjTable //邻接表 结构体
{
int nIndex;
pArcNode AdjHead;
_AdjTable():AdjHead(NULL){};
}AdjTable, AdjArray[MAX_VERTEXS+1];
Graph(int vNum = 0, int eNum = 0)
: m_vNum(vNum)
, m_eNum(eNum)
{
m_pIndgree = new int [m_vNum+1];
memset(m_pIndgree, 0, sizeof (int) * (m_vNum+1));
for (int i = 1; i <= m_vNum; ++ i)
m_AdjArray[i].nIndex = i;
}
~Graph()
{
delete [] m_pIndgree;
}
void SetIndgree(int vVertex)
{
++ m_pIndgree[vVertex];
}
void InsertAdjTable(int uVertex, int vVertex); //插入邻接表
void TopSort(); // 拓扑排序
private:
int m_vNum; //顶点数
int m_eNum;