构造无向网(邻接矩阵)

#include 
using namespace std;
#define MaxInt 32767                    
#define MVNum 100                           
#define OK 1                        
typedef char VerTexType;                      
typedef int ArcType;                          
typedef struct{ 
    VerTexType vexs[MVNum];                    
    ArcType arcs[MVNum][MVNum];               
    int vexnum,arcnum;       
}AMGraph;
int LocateVex(AMGraph G , VerTexType v){
    for(int i = 0; i < G.vexnum; ++i)
        if(G.vexs[i] == v)
            return i;
   return -1;
}
//创建无向网
int CreateUDN(AMGraph &G){ 
    int i , j , k;
    cout <<"总顶点数 总边数:";
    cin >> G.vexnum >> G.arcnum;                        
    for(i = 0; i < G.vexnum; ++i){   
        cout << "第" << (i+1) << "个点的名称:";
        cin >> G.vexs[i];                                    
    }
    for(i = 0; i < G.vexnum; ++i)                             
        for(j = 0; j < G.vexnum; ++j)   
            G.arcs[i][j] = MaxInt;  
    for(k = 0; k < G.arcnum;++k){                            
        VerTexType v1 , v2;
        ArcType w;
        cout << "第" << (k + 1) << "条边依附的顶点及权值:";
        cin >> v1 >> v2 >> w;                                
        i = LocateVex(G, v1);  j = LocateVex(G, v2);         
        G.arcs[i][j] = w;                                     
        G.arcs[j][i] = G.arcs[i][j];                         
    }    
    return OK; 
}
int main(){
    AMGraph G;     
    int i , j;
    CreateUDN(G);
    for(i = 0 ; i < G.vexnum ; ++i){
        for(j = 0; j < G.vexnum; ++j){
            if(j != G.vexnum - 1){
                if(G.arcs[i][j] != MaxInt)
                    cout << G.arcs[i][j] << "\t";
                else
                    cout << "∞" << "\t";
            }
            else{
                if(G.arcs[i][j] != MaxInt)
                    cout << G.arcs[i][j] <

image
`

你可能感兴趣的:(数据结构,c++)