1.0 头文件
#pragma once
/*
* 邻接矩阵实现图的存储
*/
// 图的最大顶点数
const int MAX_VERTEX = 10;
typedef int DataType;
// 边矩阵节点
struct ALAdjustNode
{
// 顶点序号
int vertxNumber;
// 边的权值
int right;
// 边矩阵下一个节点的地址
struct ALAdjustNode* next;
};
// 邻接数组
struct ALVertexNode
{
// 邻接编号
int vertxNumber;
// 第一个节点
struct ALAdjustNode* firstEdge;
};
class ALGraph
{
private:
// 邻接矩阵的顶点数和边数目
int vertexNum;
int arcNum;
// 邻接
ALVertexNode adjList[MAX_VERTEX];
public:
ALGraph();
~ALGraph();
ALGraph(DataType v[], int vertexNum, int arcNum);
public:
// 创建图的时候输入图的信息
void inputGraph();
};
2.0 源文件
#include "ALGraph.h"
#include <iostream>
ALGraph::ALGraph()
{
}
ALGraph::~ALGraph()
{
}
ALGraph::ALGraph(DataType v[], int vertexNum, int arcNum)
{
this->vertexNum = vertexNum;
this->arcNum = arcNum;
// 初始化邻接链表
for (int i = 0; i < arcNum; ++i)
{
adjList[i].vertxNumber = v[i];
adjList[i].firstEdge = nullptr;
}
for (int i = 0; i < this->arcNum; i++)
{
inputGraph();
}
}
// 创建图
void ALGraph::inputGraph()
{
int v;
int e;
std::cout << "请输入图的邻接点" << std::endl;
std::cin >> v >> e;
// 创建边表节点
ALAdjustNode* node = new ALAdjustNode();
node->next = nullptr;
node->right = 0;
node->vertxNumber = v;
node->next = adjList[v].firstEdge;
adjList[v].firstEdge = node;
}
3.0 测试代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ALGraph.h"
//计算逆波兰表达式的值
int main(int argc, char **argv)
{
ALGraph* graph = new ALGraph(v, 4, 4);
system("pause");
return 0;
}