Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  C语言  >  正文 C 邻接表实现无向图实例代码

C 邻接表实现无向图实例代码

发布时间:2018-09-21   编辑:www.jquerycn.cn
jquery中文网为您提供C 邻接表实现无向图实例代码等资源,欢迎您收藏本站,我们将为您提供最新的C 邻接表实现无向图实例代码资源
邻接表图的常用储存结构之一。邻接表由表头结点和表结点两部分组成,其中图中每个顶点均对应一个存储在数组中的表头结点。边没有方向的图称为无向图。本文我们用C 邻接表实现无向图。

用向量来存储一条邻接链表,存储可连通值。实现了判断是否连通,添加边,添加顶点的功能。

UnDirectGraph.h

#pragma once

include “stdafx.h”
include <vector>

using namespace std;
class UnDirectGraph
{
private:
int vCount;
vector<int> *adj;
public:
int GetVCount();
UnDirectGraph(int vCount);
void AddEdge(int v,int w);
vector<int> &Vadj(int v);
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<int>[vCount];

for (int i=0;i&lt;vCount;i )
{
        adj[i].clear();
}

}
void UnDirectGraph::AddEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}

vector<int>& UnDirectGraph::Vadj(int v)
{
return adj[v];
}

bool UnDirectGraph::IsConnected(int v,int w)
{
for (vector<int>::iterator iter=adj[v].begin();iter!=adj[v].end();iter )
{
if (*iter==w)
{
return true;
}
}
return false;
}

int UnDirectGraph::GetVCount()
{
return vCount;
}

代码还算清晰,就不解释了,相信学习C 的同学都看得懂。

您可能感兴趣的文章:
C 邻接表实现无向图实例代码
C语言 拓扑排序算法解析
python graph什么意思
php用不了推荐算法吗
老师不会教的【DIV间距设置】技巧
电脑本地连接受限制或无连接的解决办法
可变分区回收算法
JAVA List使用Remove时的一些问题
css选择器使用方法总结
利用HTML5的details, summary实现各种交互效果

[关闭]