DFSB(G,n);
//system("pause");
return0;
}
3.对学生选课工程图进行拓扑排序.
#include
#include
#defineMAX_VEXTEX_NUM20
#defineM20
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
#defineOK1
#defineERROR0
typedefintElemType;
typedefstructArcNode
{
intadjvex;
structArcNode*nextarc;
}ArcNode;
typedefstructVNode
{
intdata;
ArcNode*firstarc;
}VNode,AdjList[MAX_VEXTEX_NUM];
typedefstruct
{
AdjListvertices;
intvexnum,arcnum;
}ALGraph;
typedefstruct//构件栈
{
ElemType*base;
ElemType*top;
intstacksize;
}SqStack;
voidInitStack(SqStack*);//函数声明
intPop(SqStack*,ElemType*);
voidPush(SqStack*,ElemType);
intStackEmpty(SqStack*);
voidCreatGraph(ALGraph*);
voidFindInDegree(ALGraph,int*);
voidTopologicalSort(ALGraph);
voidInitStack(SqStack*S)//初始化栈
{
S->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!
S->base)
{
printf("memoryallocationfailed,goodbye");
exit
(1);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
intPop(SqStack*S,ElemType*e)//出栈操作
{
if(S->top==S->base)
{returnERROR;}
*e=*--S->top;
//printf("%d\n",e);
//returne;
return0;
}
voidPush(SqStack*S,ElemTypee)//进栈操作
{if(S->top-S->base>=S->stacksize)
{
S->base=(ElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!
S->base)
{
printf("memoryallocationfailed,goodbye");
exit
(1);
}
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}*S->top++=e;
}
intStackEmpty(SqStack*S)//判断栈是否为空
{
if(S->top==S->base)
returnOK;
else
returnERROR;}
voidCreatGraph(ALGraph*G)//构件图
{intm,n,i;
ArcNode*p;
printf("请输入顶点数和边数:
");
scanf("%d%d",&G->vexnum,&G->arcnum);
for(i=1;i<=G->vexnum;i++)
{G->vertices[i].data=i;
G->vertices[i].firstarc=NULL;
}
for(i=1;i<=G->arcnum;i++)//输入存在边的点集合
{
printf("\n请输入存在边的两个顶点的序号:
");
scanf("%d%d",&n,&m);
while(n<0||n>G->vexnum||m<0||m>G->vexnum)
{printf("输入的顶点序号不正确请重新输入:
");
scanf("%d%d",&n,&m);
}
p=(ArcNode*)malloc(sizeof(ArcNode));
if(p==NULL)
{printf("memoryallocationfailed,goodbey");
exit
(1);
}
p->adjvex=m;
p->nextarc=G->vertices[n].firstarc;
G->vertices[n].firstarc=p;
}
printf("建立的邻接表为:
\n");//输出建立好的邻接表
for(i=1;i<=G->vexnum;i++)
{
printf("%d",G->vertices[i].data);
for(p=G->vertices[i].firstarc;p;p=p->nextarc)
printf("%3d",p->adjvex);
printf("\n");
}}
voidFindInDegree(ALGraphG,intindegree[])//求入度操作
{
inti;
for(i=1;i<=G.vexnum;i++)
{
indegree[i]=0;
}
for(i=1;i<=G.vexnum;i++)
{while(G.vertices[i].firstarc)
{indegree[G.vertices[i].firstarc->adjvex]++;
G.vertices[i].firstarc=G.vertices[i].firstarc->nextarc;
}
}
}
voidTopologicalSort(ALGraphG)//进行拓扑排序
{
intindegree[M];
inti,k,n;
intcount=0;
ArcNode*p;
SqStackS;
FindInDegree(G,indegree);
InitStack(&S);
for(i=1;i<=G.vexnum;i++)
{
printf("第%d个点的入度为%d\n",i,indegree[i]);
}
printf("\n");
for(i=1;i<=G.vexnum;i++)
{
if(!
indegree[i])
Push(&S,i);
}
printf("进行拓扑排序输出顺序为:
");//输出结果
while(!
StackEmpty(&S))
{
Pop(&S,&n);
printf("%4d",G.vertices[n].data);
count++;
for(p=G.vertices[n].firstarc;p!
=NULL;p=p->nextarc)
{
k=p->adjvex;
if(!
(--indegree[k]))
{
Push(&S,k);
}
}
}printf("\n");
if(count{
printf("出现错误\n");
}
else
{
printf("排序成功\n");
}
}
intmain(void)//主函数
{
ALGraphG;
CreatGraph(&G);
TopologicalSort(G);
system("pause");
return0;
}