为了输出美观,此处定义MaxInt为9.
#include <iostream>
#define MaxInt 9
#define MVNum 100
#define MAX_SIZE 100
/**
邻接矩阵实现无向网
*/
typedef char VerTexType;
typedef int ArcType;
using namespace std;
typedef struct {
VerTexType vexs[10];
ArcType arcs[10][10];
int vexnum; //point
int arcnum; //
}AMGraph;
int init(AMGraph &G){
for(int i=0;i<G.vexnum;i++){
for(int j=0;j<G.vexnum;j++){
G.arcs[i][j] = MaxInt;
}
}
}
/**
*带有权重的有向网创建,需要按需输入相关信息,有提示。
*/
int CreatUDN(AMGraph &G){
//init(G);
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
G.arcs[i][j] = MaxInt;
}
}
cout<<"please input vexnum and arcnum:"<<endl;
cin>>G.vexnum>>G.arcnum;
for(int i=0;i<G.vexnum;i++){
cout<<"please input the "<<i<<" point"<<endl;
cin>>G.vexs[i];
}
for(int i=0;i<G.arcnum;i++){
VerTexType v1,v2;
int w = 0;
int w1 = 0;
int w2 = 0;
cout<<"please input v1 ,v2 and w "<<""<<endl;
cin>>v1>>v2>>w;
for(int j=0;j<G.vexnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = w;
G.arcs[w2][w1] = w;
}
return 1;
}
/**
无向网络 增加边
*/
int InsertARC(AMGraph G,int num){
VerTexType v1,v2;
int w1,w2;
int w;
cout<<"please input v1 ,v2 and w "<<""<<endl;
for(int i=0;i<num;i++){
for(int j=0;j<G.arcnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = w;
G.arcs[w2][w1] = w;
}
}
/**
无向网络 删除边
*/
int DeleteARC(AMGraph G,int num){
VerTexType v1,v2;
int w1,w2;
int w;
cout<<"please input v1 ,v2 and w "<<""<<endl;
for(int i=0;i<num;i++){
for(int j=0;j<G.arcnum;j++){
if(v1==G.vexs[j]) w1 = j;
if(v2==G.vexs[j]) w2 = j;
}
G.arcs[w1][w2] = MaxInt;
}
}
/**
无向网络 矩阵显示功能
*/
void UDN_dis(AMGraph G){
cout<<" ";
for(int i=0;i<G.vexnum;i++){
cout<<G.vexs[i]<<" ";
}
cout<<endl;
for(int i=0;i<G.vexnum;i++){
cout<<G.vexs[i]<<" ";
for(int j=0;j<G.vexnum;j++){
cout<<G.arcs[i][j]<<" ";
}
cout<<endl;
}
cout<<"Display end!"<<endl;
}
//int main()
//{
// AMGraph amg;
//
// CreatUDN(amg);
// UDN_dis(amg);
//
// return 0;
//}
/**
6 5
a b c d e f
a b 1 a c 1 c d 1 c e 1 d f 1
*/