//严蔚敏《数据结构》
//图:拓扑排序
//自学中,加油!!
#include<iostream>
using namespace std;
#define InfoType double
#define VertexType string
const int MaxVertexNum=20;
typedef struct ArcBox
{
int tailvex,headvex;//该弧的尾邻接点、头邻接点位置下标
struct ArcBox* tlink,* hlink;//分别为弧尾相同、弧头相同的弧的链域
InfoType* info;
}ArcBox;//弧
typedef struct VexNode
{
VertexType data;
ArcBox* firstin,* firstout;
}VexNode;
typedef struct
{
VexNode xlist[MaxVertexNum];
int vexnum,arcnum;
}OLGraph;
int Locate_xlist(OLGraph G,VertexType v)
{
for(int i=0;i!=G.vexnum;i++){
if(G.xlist[i].data==v)
return i;
}
cout<<"未找到\n";
return -1;
}
bool Creat_OLGraph(OLGraph& G)//用十字链表创建有向网
{
cout<<"输入有向网的顶点数和弧数:";
cin>>G.vexnum>>G.arcnum;
cout<<"输入"<<G.vexnum<<"个顶点信息\n";
for(int i=0;i!=G.vexnum;i++){//输入的同时对十字链表进行初始化
cin>>G.xlist[i].data;
G.xlist[i].firstin=G.xlist[i].firstout=nullptr;
}
VertexType v1,v2;
InfoType w;
int i,j;
ArcBox* p;
for(int k=0;k!=G.arcnum;k++){
cout<<"输入两个顶点以及权值\n";
cin>>v1>>v2>>w;
i=Locate_xlist(G,v1);
j=Locate_xlist(G,v2);
p=new ArcBox;
if(!p)
return false;
p->tailvex=i;p->headvex=j;
p->info=new InfoType;
*p->info=w;
p->tlink=G.xlist[i].firstout;
G.xlist[i].firstout=p;
p->hlink=G.xlist[j].firstin;
G.xlist[j].firstin=p;
}
return true;
}
void Output_OLGraph(OLGraph G)
{
ArcBox* p;
cout<<"各个顶点的出度\n";
for(int i=0;i!=G.vexnum;i++){
cout<<"顶点信息:"<<G.xlist[i].data;
p=G.xlist[i].firstout;
while(p){
cout
2020-10-15 //严蔚敏《数据结构》 //图:拓扑排序
最新推荐文章于 2024-07-06 19:23:57 发布
本文深入探讨了严蔚敏《数据结构》中关于图的拓扑排序理论,通过实例解析拓扑排序的过程及其在实际问题中的应用。
摘要由CSDN通过智能技术生成