邻接表法
#include <iostream>
#include <string>
constexpr int MaxVertaxNum = 100;
//边表结点
struct ArcNode {
int adjvex;
ArcNode* next;
};
//顶点表结点
struct VNode {
std::string data;
ArcNode* first;
};
//邻接表存储的图类型
struct ALGraph {
VNode Vertices[MaxVertaxNum];
int arcnum;
int vexnum;
bool directed;
};
//初始化
void InitGraph (ALGraph& G, bool directed) {
G.arcnum = 0;
G.arcnum = 0;
G.directed = directed;
}
// 按顶点值查找其下标。时间复杂度 O(|V|)
int LocateVex (const ALGraph& G, const std::string value) {
for (int i = 0; i < G.vexnum; ++i) {
if (G.Vertices[i].data == value) {
return i;
}
}
return -1;
}
// 插入顶点。成功返回其下标;重名、顶点表已满时返回 -1。
// 时间复杂度 O(|V|),其中查重占主要部分。
int InsertVertex (ALGraph& G, const std::string value) {
if (G.vexnum >= MaxVertaxNum || LocateVex(G, value) != -1) {
return -1;
}
int index = G.vexnum++;
G.Vertices[index].data = value;
G.Vertices[index].first = nullptr;
return index;
}
//判断下标是否合法。
bool ValidVertex(const ALGraph& G, int v) {
return v >= 0 && v < MaxVertaxNum;
}
//判断 v 到 w 是否存在边。
//无需判断v与w是否相同
bool Adjacent(const ALGraph& G, int v, int w) {
if (!ValidVertex(G, v) || !ValidVertex(G, w)) {
return -1;
}
for (ArcNode *p = G.Vertices[v].first; p != nullptr; p = p->next) {
if (p->adjvex == w) {
return 1;
}
}
return -1;
}
int main() {
return 0;
}
不能在继续记代码,写代码题了。
太浪费时间,得赶紧快进到广度深度优先部分。
现在开学了,还有三本书没过,真是头疼😭。
只能之后有时间了慢慢更新,反正过段时间就忘了。
智力水平重新退化到奶龙水平。
