MatGraph.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
#define MAXV 7 //最大顶点个数
#define INF 32767 //表示∞
typedef int InfoType;
typedef char VertexType;
//邻接矩阵存储定义
typedef struct//图的定义
{
//int no;//顶点编号
int edges[MAXV][MAXV];//邻接矩阵
int n, e;//顶点数,边数
VertexType vexs[MAXV];//存放顶点信息
}MatGraph;
//图的邻接表存储定义
typedef struct ANode
{
int adjvex;//该边的终点编号
struct ANode* nextarc;//指向下一条边的指针
InfoType weight;//该边的权值等信息
}ArcNode;
typedef char Vertex[10];
typedef struct Vnode
{
Vertex data;//顶点信息
ArcNode* firstarc;//指向第一条边
}VNode;
typedef struct
{
VNode adjlist[MAXV];//邻接表
int n1, e1;//顶点数n和边数e
}AdjGraph;
//初始化邻接矩阵
void InitMGraph(MatGraph* G);
//建立邻接矩阵
void CreateMGraph(MatGraph* G);
//输出邻接矩阵
void DispMGraph(MatGraph* G, int n);
//初始化邻接表
void InitALGraph(AdjGraph* A);
//建立邻接表AdjGraph
void CreateALGraph(AdjGraph*& A);
//输出邻接表
void DispALGraph(AdjGraph* A, ArcNode* L);
MatGraph.cpp
#include "MatGraph.h"
void InitMGraph(MatGraph* G)
{
G = (MatGraph*)malloc(sizeof(MatGraph));
//G->no=G->n = G->e = 0;
for (int i = 0; i < MAXV; i++)
{
G->vexs[i] = 0;
for (int j = 0; j < MAXV; j++)
{
G->edges[i][j] = 0;
}
}
}
void CreateMGraph(MatGraph* G)
{
G = (MatGraph*)malloc(sizeof(MatGraph));
int i, j, k, w,n1=0;
cout << "请输入顶点数和边数:";
cin >> G->n >> G->e;
n1 = G->n;
for (i = 0; i < G->n; i++)
{
for (j = 0; i < G->n; j++)
G->edges[i][j] = 0;//矩阵初始化
}
cout << "输入边(vi,vj)的下标i,j和权w(eg.0 1表示0与1之间有一条边):"<<endl;
for (k = 0; k < G->e; k++)
{
cin >> i >> j >> w;
G->edges[i][j] = w;
G->edges[j][i] = G->edges[i][j];
}
G->n = n1;
DispMGraph(G,G->n);
}
void DispMGraph(MatGraph* G,int n)
{
cout << endl;
cout << "图的邻接矩阵:" << endl;
cout << " " ;
cout << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << G->edges[i][j]<<'\t';
cout << endl;
}
cout << endl;
}
//初始化邻接表
void InitALGraph(AdjGraph* A)
{
A = (AdjGraph*)malloc(sizeof(AdjGraph));
A->n1 = 0;
A->e1 = 0;
}
//建立邻接表AdjGraph
void CreateALGraph(AdjGraph*& A)
{
A = (AdjGraph*)malloc(sizeof(AdjGraph));
int i, j = 0,w=0;
ArcNode* L=0;
cout << "请输入顶点数和边数:";
cin >> A->n1 >> A->e1;
//cout << "输入顶点信息(A B C...):";
for (i = 0; i < A->n1; i++)
{
//cin >> A->adjlist[i].data;//输入顶点信息
A->adjlist[i].firstarc = 0;//将邻接表初始化
}
cout << "输入边(vi,vj)上的顶点序号以及权值:";
for (int k = 0; k < A->e1; k++)
{
cin >> i >> j>>w;
L = (ArcNode*)malloc(sizeof(ArcNode));
L->adjvex = j;//边的终点编号
L->nextarc = A->adjlist[i].firstarc;
L->weight = w;
A->adjlist[i].firstarc = L;//将当前顶点的指针指向L
L = (ArcNode*)malloc(sizeof(ArcNode));
L->adjvex = i;
L->nextarc = A->adjlist[j].firstarc;
L->weight = w;
A->adjlist[j].firstarc = L;
}
cout <<endl;
DispALGraph(A,L);
}
//输出邻接表
void DispALGraph(AdjGraph* A,ArcNode*L)//
{
cout << "图的邻接表:" << endl;
for (int i = 0; i < A->n1; i++)
{
L = A->adjlist[i].firstarc;
cout << i << " ";
while (L)
{
cout << "→" << L->adjvex << " " << L->weight<<"(权重)";
L = L->nextarc;
}
cout << endl;
}
}
Graph.cpp
#include "MatGraph.h"
int main()
{
MatGraph* G=0;
InitMGraph(G);
CreateMGraph(G);
//DispGraph(G);
AdjGraph* A=0;
InitALGraph(A);
CreateALGraph(A);
}