最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C++邻接表实现无向图实例代码
时间:2022-06-25 04:50:50 编辑:袖梨 来源:一聚教程网
用向量来存储一条邻接链表,存储可连通值。实现了判断是否连通,添加边,添加顶点的功能。
UnDirectGraph.h
#pragma once
include “stdafx.h”
include
using namespace std;
class UnDirectGraph
{
private:
int vCount;
vector
public:
int GetVCount();
UnDirectGraph(int vCount);
void AddEdge(int v,int w);
vector
bool IsConnected(int v,int w);
};
UnDirectGraph.cpp
#pragma once
include “stdafx.h”
include “UnDirectGraph.h”
using namespace std;
UnDirectGraph::UnDirectGraph(int _vCount)
{
this->vCount=_vCount;
adj=new vector
for (int i=0;i<vCount;i++)
{
adj[i].clear();
}
}
void UnDirectGraph::AddEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
vector
{
return adj[v];
}
bool UnDirectGraph::IsConnected(int v,int w)
{
for (vector
{
if (*iter==w)
{
return true;
}
}
return false;
}
int UnDirectGraph::GetVCount()
{
return vCount;
}
代码还算清晰,就不解释了,相信学习C++的同学都看得懂。
相关文章
- 美团外卖年度报告在哪查看 07-06
- presentation-skills 第一次试跑和结果检查教程(新手)(skill首测版) 07-06
- 原神林尼背景立绘图鉴-原神全部角色介绍 07-06
- 我的乡村日常生活冷狐破解版下载 07-06
- gpt-image2-ppt-skills 本地安装使用教程(新手)(skill安装版) 07-06
- 《息风谷战略》鹤岭门副本流程做法介绍 07-06