图的邻接矩阵表示的C++类实现
2016-06-05 15:41
323 查看
任何的抽象数据类型(Abstract Data Type)都由3要素组成,名称、数据对象集合操作集。对于图的数据结构,其名称为Graph(图),数据对象集由一个非空的有限顶点集合和一个有限边集合组成,操作集包括插入顶点、插入边等。教材上对于图的实现形式主要介绍了两种:邻接矩阵和邻接表,本文采用临街矩阵的表示图,初步实现简单的图类,可以进行创建图、插入边、打印图等操作。
//graphadjcencymatrix.h
#ifndef GRAPHADJCENCYMATRIX
#define GRAPHADJCENCYMATRIX
#include <IOSTREAM>
const int MaxVertexNum=100;
typedef int Vertex;
typedef int Weight;
typedef char DataType;
struct Edge
{
Vertex v1;
Vertex v2;
Weight w;
};
class GraphAdjMat
{
private:
int VertexNum;
int EdgeNum;
int Weight[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];
public:
GraphAdjMat();
~GraphAdjMat(){}
void InsertEdge(Edge);
void BuildGraph();
void Print();
};
GraphAdjMat::GraphAdjMat()
{
VertexNum=0;
EdgeNum=0;
for(int i=0;i<MaxVertexNum;i++)
{
Data[i]='*';
for(int j=0;j<MaxVertexNum;j++)
{
Weight[i][j]=-1;
}
}
}
void GraphAdjMat::InsertEdge(Edge temp)
{
//EdgeNum++;
Weight[temp.v1-1][temp.v2-1]=temp.w;
}
void GraphAdjMat::BuildGraph()
{
std::cout<<"Please input the vertex count:\n";
std::cin>>VertexNum;
std::cout<<"Please input the edge count:\n";
std::cin>>EdgeNum;
std::cout<<"Please input the vertices and weight of the edge:\n";
for(int i=0;i<EdgeNum;i++)
{
int a,b,c;
std::cin>>a>>b>>c;
Edge first={a,b,c};
InsertEdge(first);
}
}
void GraphAdjMat::Print()
{
int i,j;
for(i=0;i<VertexNum;i++)
std::cout<<Data[i]<<" ";
std::cout<<std::endl;
for(i=0;i<VertexNum;i++)
{
for(j=0;j<VertexNum;j++)
{
std::cout<<Weight[i][j]<<" ";
}
std::cout<<std::endl;
}
}
#endif
//graphadjcencymatrix.h
#ifndef GRAPHADJCENCYMATRIX
#define GRAPHADJCENCYMATRIX
#include <IOSTREAM>
const int MaxVertexNum=100;
typedef int Vertex;
typedef int Weight;
typedef char DataType;
struct Edge
{
Vertex v1;
Vertex v2;
Weight w;
};
class GraphAdjMat
{
private:
int VertexNum;
int EdgeNum;
int Weight[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];
public:
GraphAdjMat();
~GraphAdjMat(){}
void InsertEdge(Edge);
void BuildGraph();
void Print();
};
GraphAdjMat::GraphAdjMat()
{
VertexNum=0;
EdgeNum=0;
for(int i=0;i<MaxVertexNum;i++)
{
Data[i]='*';
for(int j=0;j<MaxVertexNum;j++)
{
Weight[i][j]=-1;
}
}
}
void GraphAdjMat::InsertEdge(Edge temp)
{
//EdgeNum++;
Weight[temp.v1-1][temp.v2-1]=temp.w;
}
void GraphAdjMat::BuildGraph()
{
std::cout<<"Please input the vertex count:\n";
std::cin>>VertexNum;
std::cout<<"Please input the edge count:\n";
std::cin>>EdgeNum;
std::cout<<"Please input the vertices and weight of the edge:\n";
for(int i=0;i<EdgeNum;i++)
{
int a,b,c;
std::cin>>a>>b>>c;
Edge first={a,b,c};
InsertEdge(first);
}
}
void GraphAdjMat::Print()
{
int i,j;
for(i=0;i<VertexNum;i++)
std::cout<<Data[i]<<" ";
std::cout<<std::endl;
for(i=0;i<VertexNum;i++)
{
for(j=0;j<VertexNum;j++)
{
std::cout<<Weight[i][j]<<" ";
}
std::cout<<std::endl;
}
}
#endif
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- 渗透技术一瞥(图)
- 图片引发的溢出危机(图)
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例