資源簡介
拓撲排序算法,用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
評論
共有 條評論