资源简介
拓扑排序算法,用C++写的,有注释,适合初学者。
代码片段和文件信息
#include
#include
#define MAX_VEXTEX_NUM 20 //最大顶点个数#define M 20
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define M 20
#define ERROR 0
typedef int ElemType;
typedef struct ArcNode //定义表结点结构
{
int adjvex; //与vi相邻接的顶点编号
struct ArcNode *nextarc; //指向下一条弧(边)的指针
}ArcNode;
typedef struct VNode //定义表头结点结构
{
int data;
ArcNode *firstarc; //指向第一条弧(边)的指针
}VNodeAdjList[MAX_VEXTEX_NUM];
typedef struct //定义邻接表结构
{
AdjList vertices; //表头结点数组
int vexnum arcnum; //顶点和弧(边)的个数
}ALGraph;
typedef struct //构件栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
void InitStack(SqStack *); //函数声明
int Pop(SqStack * ElemType *);
void Push(SqStack *ElemType );
int StackEmpty(SqStack *);
void CreatGraph(ALGraph *);
void FindInDegree(ALGraph int * );
void TopologicalSort(ALGraph );
void InitStack(SqStack *S)//初始化栈
{
S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base)
{
printf(“memory allocation failed goodbye“);
exit(1);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
int Pop(SqStack *SElemType *e)//出栈操作
{
if(S->top==S->base)
{
return ERROR;
}
*e=*--S->top;
return 0;
}
void Push(SqStack *SElemType e)//进栈操作
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base)
{
printf(“memory allocation failed goodbye“);
exit(1);
}
S->top = S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
}
int StackEmpty(SqStack *S)//判断栈是否为空
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
void CreatGraph(ALGraph *G)//构建图
{
int m n i;
ArcNode *p;
printf(“请输入顶点数和边数:“);
scanf(“%d %d“&G->vexnum&G->arcnum);
for (i = 1; i <= G->vexnum; i++)
{
G->ve
- 上一篇:zxing库c++)
- 下一篇:AGC 自动增益控制demo
评论
共有 条评论