无向网的邻接矩阵存储方式-标准版本

 为了输出美观,此处定义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
*/

 

转载于:https://my.oschina.net/lafter/blog/911287

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值