데이터 구조 (9) - 그림 의 인접 행렬 저장

7055 단어 데이터 구조
////////////////////////////////////////////////////////

//        

////////////////////////////////////////////////////////

#include <iostream>

#include <stdlib.h>

#define MaxVertexNum 100 //     

#define INFINITY 0  //              

typedef char VertexType;  //          

typedef int EdgeType; ///        

enum GraphType{DG, UG, DN, UN}; //   ,   ,    ,    

using namespace std;



typedef struct

{

    VertexType Vertices[MaxVertexNum]; //   

    EdgeType Edges[MaxVertexNum][MaxVertexNum]; //

    int n, e; //   n   e

    enum GraphType GType; 

}MGraph;



void CreateMGraph(MGraph *G)

{

    int  i, j, k, w;

    G->GType = UN;    /* Undirected Network        */

    cout << "         (     :   ,   ):" << endl;

    cin >> G->n >> G->e; /*          */

    cout << "       (     :   <CR>):" << endl;

    for (i = 0; i < G->n; i++)

        cin >> &(G->Vertices[i]); /**/

    for (i = 0; i < G->n; i++)

        for (j = 0; j < G->n; j++)

            G->Edges[i][j] = INFINITY; /*         */

    cout << "                   ,     :i, j, w:" << endl;

    for (k = 0; k < G->e; k++) {

        cin >> i >> j >> w; /*   e     ,       */

        G->Edges[i][j] = w;

        G->Edges[j][i] = w; /*                 */

    }

}



void Print(MGraph *G)

{

    cout << "   ";

    for (int i = 0; i < G->n; i++)

        cout << G->Vertices[i] << "  ";

    cout << endl;

    for (int i = 0; i < G->n; i++)

    {

        cout << G->Vertices[i] << "  ";

        for (int j = 0; j < G->n; j++)

        {

            cout << G->Edges[i][j] << "  ";

        }

        cout << endl;

    }

}



int main()

{

    MGraph *G = new MGraph;

    CreateMGraph(G);

    Print(G);

    int i = 0;

    //scanf("%d", &i);

    system("pause");

    return 0;

}

좋은 웹페이지 즐겨찾기