基于邻接表实现简单图的一些基本操作
接着昨天的记录,上次是邻接矩阵实现,这次是邻接表实现的,废话不多说,直接上代码
C/C++ 代码:
ALGraph.h 代码:
#include<iostream>
#include<cstring>
#include<malloc.h>
#include"LinkQueue.h"
using namespace std;
#define MAX_VERTEX_SIZE 20
#ifndef __ALGRAPH_H__
#define __ALGRAPH_H__
template <class E> struct ArcNode {
int adjvex;
E info;
struct ArcNode *next;
};
template <class V, class E> struct VexNode {
V data;
ArcNode<E> *first;
};
template <class V, class E> struct ALGraph {
int vexnum;
int arcnum;
bool isWeighted;
bool isUndir;
VexNode<V, E> vex[MAX_VERTEX_SIZE];
};
template <class V, class E> void InitALGraph(ALGraph<V, E> *&g) {
g = (ALGraph<V, E> *)malloc(sizeof(ALGraph<V, E>));
g -> vexnum = 0;
g -> arcnum = 0;
g -> isWeighted = false;
g -> isUndir = true;
for(int i = 0; i < MAX_VERTEX_SIZE; i++) {
g -> vex[i].data = 0;
g -> vex[i].first = NULL;
}
}
template <class V, class E> void CreateALGraph(ALGraph<V, E> *&g) {
char ch;
V x, y;
E z = NULL;
InitALGraph(g);
cout<<"Do you want to create a weighted graph ? (y / n) ";
cin>>ch;
g -> isWeighted = ch == 'y' || ch == 'Y' ? true : false;
cout<<"Do you want to create a undirected graph ? (y / n) ";
cin>>ch;
g -> isUndir = ch == 'y' || ch == 'Y' ? true : false;
cout<<"Please input the number of vertices and edges: ";
cin>>g -> vexnum>>g -> arcnum;
cout<<"Please input vertices:"<<endl;
for(int i = 0; i < g -> vexnum; i++) {
cin>>g -> vex[i].data;
}
cout<<"Please input a pair of adjacent points (and weight if necessary):"<<endl;
for(int k = 0; k < g -> arcnum; k++) {
cin>>x>>y;
if(g -> isWeighted) {
cin>>z;
}
int i = LocateVex(g, x);
int j = LocateVex(g, y);
ArcNode<E> *p = (ArcNode<E> *)malloc(sizeof(ArcNode<E>));
p -> adjvex = j;
p -> info = z;
p -> next = g -> vex[i].first;
g -> vex[i].first = p;
if(g -> isUndir) {
p = (ArcNode<E> *)malloc(sizeof(ArcNode<E>));
p -> adjvex = i;
p -> info = z;
p -> next = g -> vex[j].first;
g -> vex[j].first = p;
}
}
cout<<"Create MGraph success!"<<endl;
}
template <class V> void visit(V v) {
cout<<v<<" ";
}
template <class V, class E> void PrintALGraph(ALGraph<V, E> *g) {
ArcNode<E> *p;
cout<<"ALGraph:"<<endl;
for(int i = 0; i < g -> vexnum; i++) {
p = g -> vex[i].first;
cout<<g -> vex[i].data<<" -> ";
while(p != NULL) {
cout<<"["<<g -> vex[p -> adjvex].data;
if(g -> isWeighted) {
cout<<":"<<p -> info;
}
cout<<"] -> ";
p = p -> next;
}
cout<<"NULL"<<endl;
}
cout<<endl;
}
template <class V, class E> int LocateVex(ALGraph<V, E> *g, V v) {
for(int i = 0; i < g -> vexnum; i++) {
if(v == g -> vex[i].data) {
return i;
}
}
return -1;
}
template <class V, class E> bool Adjacent(ALGraph<V, E> *g, V v1, V v2) {
int i = LocateVex(g, v1);
int j = LocateVex(g, v2);
if(i == -1 || j == -1) {
return false;
}
ArcNode<E> *p = g -> vex[i].first;
while(p != NULL) {
if(v2 == g -> vex[p -> adjvex].data) {
return true;
}
p = p -> next;
}
return false;
}
template <class V, class E> V FirstAdjVex(ALGraph<V, E> *g, V v) {
int index = LocateVex(g, v);
ArcNode<E> *p = g -> vex[index].first;
return index == -1 || p == NULL ? NULL : g -> vex[p -> adjvex].data;
}
template <class V, class E> V NextAdjVex(ALGraph<V, E> *g, V v1, V v2) {
int index = LocateVex(g, v1);
if(index == -1) {
return false;
}
ArcNode<E> *p = g -> vex[index].first;
while(p != NULL) {
if(v2 == g -> vex[p -> adjvex].data) {
p = p -> next;
return p == NULL ? NULL : g -> vex[p -> adjvex].data;
}
p = p -> next;
}
return NULL;
}
template <class V, class E> void BFS(ALGraph<V, E> *g, V v, bool *isVisit) {
visit<V>(v);
isVisit[LocateVex(g, v)] = true;
LinkQueue<V> *q;
InitQueue(q);
EnQueue(q, v);
while(!QueueEmpty(q)) {
DeQueue(q, v);
for(V w = FirstAdjVex(g, v); w != NULL; w = NextAdjVex(g, v, w)) {
if(!isVisit[LocateVex(g, w)]) {
visit<V>(w);
isVisit[LocateVex(g, w)] = true;
EnQueue(q, w);
}
}
}
}
template <class V, class E> void BFS(ALGraph<V, E> *g) {
bool *isVisit = new bool[g -> vexnum] {false};
for(int i = 0; i < g -> vexnum; i++) {
if(!isVisit[i]) {
BFS(g, g -> vex[i].data, isVisit);
}
}
}
template <class V, class E> void DFS(ALGraph<V, E> *g, V v, bool *isVisit) {
visit<V>(v);
isVisit[LocateVex(g, v)] = true;
for(V w = FirstAdjVex(g, v); w != NULL; w = NextAdjVex(g, v, w)) {
if(!isVisit[LocateVex(g, w)]) {
DFS(g, w, isVisit);
}
}
}
template <class V, class E> void DFS(ALGraph<V, E> *g) {
bool *isVisit = new bool[g -> vexnum] {false};
for(int i = 0; i < g -> vexnum; i++) {
if(!isVisit[i]) {
DFS(g, g -> vex[i].data, isVisit);
}
}
}
template <class V, class E> void DestroyALGraph(ALGraph<V, E> *g) {
ArcNode<E> *p;
for(int i = 0; i < g -> vexnum; i++) {
p = g -> vex[i].first;
while(p != NULL) {
g -> vex[i].first = p -> next;
free(p);
p = g -> vex[i].first;
}
}
free(g);
}
#endif