#include "stdafx.h"
#include <iostream.h>
#include <malloc.h>

int const vnum=6;
//单链表定义
typedef struct arcnode


{
int adjvex; //与其相连的下个结点的编号
struct arcnode * nextarc; //指向下一个结点的指针
}ArcNodeTp;

//顶点定义
typedef struct vexnode


{
int vertex; //顶点的值
int in; //入度数量
ArcNodeTp * firstarc; //指向的出度的单链表
}AdjList[vnum];

//图的定义
typedef struct graph


{
AdjList adjlist;
int vexnum,arcnum;
}GraphTp;

//建立一个新的结点
ArcNodeTp * CreateNewNode(int adjvex)


{
ArcNodeTp * node=(ArcNodeTp *)malloc(sizeof(ArcNodeTp));
node->adjvex=adjvex;
node->nextarc=NULL;

return node;
}

//建立P122的图5-17(a)
void CreateGraph(GraphTp & graph)


{
graph.arcnum=7;
graph.vexnum=vnum;

ArcNodeTp * p=NULL;
int index=0;
//建立结点
index=0;
graph.adjlist[index].vertex=index;
p=CreateNewNode(1);
//修改头结点
graph.adjlist[index].firstarc=p;
//指向下一个结点
p->nextarc=CreateNewNode(2);
graph.adjlist[index].in=0;

index=1;
graph.adjlist[index].vertex=index;
p=CreateNewNode(5);
graph.adjlist[index].firstarc=p;
graph.adjlist[index].in=1;

index=2;
graph.adjlist[index].vertex=index;
p=CreateNewNode(5);
graph.adjlist[index].firstarc=p;
graph.adjlist[index].in=2;

index=3;
graph.adjlist[index].vertex=index;
p=CreateNewNode(2);
graph.adjlist[index].firstarc=p;
p->nextarc=CreateNewNode(4);
graph.adjlist[index].in=0;

index=4;
graph.adjlist[index].vertex=index;
p=CreateNewNode(5);
graph.adjlist[index].firstarc=p;
graph.adjlist[index].in=1;

index=5;
graph.adjlist[index].vertex=index;
graph.adjlist[index].firstarc=NULL;
graph.adjlist[index].in=3;
}

void Top_Sort(GraphTp g)


{
//用数组和一个变量模拟栈
int stack[vnum];
int top=0;
int i=0;
for(i=0;i<g.vexnum;i++)

{
if (g.adjlist[i].in==0)

{
stack[++top]=i;
}
}
int count=0;
while(top!=0)

{
int v=stack[top--];
count++;
cout<<"输出顶点:"<<g.adjlist[v].vertex<<endl;
ArcNodeTp * p=g.adjlist[v].firstarc;
//此循环目的是去掉此弧尾对应的顶点的边,实现的方法并非删除,则是将出度的对应的顶点的In-1
while(p!=NULL)

{
//将弧头的入度数减一后,判断是否入度为0,如果为0则入栈
if (--g.adjlist[p->adjvex].in==0)

{
stack[++top]=p->adjvex;
}
p=p->nextarc;
}
}
if (count<g.vexnum)
cout<<"图中存在环"<<endl;
else
cout<<"拓扑排序完成"<<endl;
}

int main(int argc, char* argv[])


{
GraphTp g;
CreateGraph(g);
Top_Sort(g);
return 0;
}
另外,书在首次将入度为0的顶点编号入栈的地方没有写"{",这样的结果是错误的,估计是排版的人弄错的.可以参考我的代码,填上相应的"{}"
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步