利用普通链表构建的邻接表
核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| bool visted[MaxV];
void dfs(AdjGraph* G, int v) { ArcNode* p; visted[v] = true; cout << v << " -> "; p = G->adjlist[v].firstarc; while (p != NULL) { if (!visted[p->adjvex]) { dfs(G, p->adjvex); } p = p->nextarc; } }
|
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <iostream> #define MaxV 100010 #define INF INT_MAX
using namespace std;
typedef string infoType;
typedef struct ANode { int adjvex; struct ANode* nextarc; int weight; } ArcNode;
typedef struct { infoType info; ArcNode* firstarc; } VNode;
typedef struct { VNode adjlist[MaxV]; int n, e; } AdjGraph;
bool visted[MaxV];
void CreateAdj(AdjGraph*& G,int A[MaxV][MaxV], int n, int e) { ArcNode *p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (int i = 0; i < n; ++i) G->adjlist[i].firstarc = NULL; for (int i = 0; i < n; ++i) { for (int j = n-1; j >= 0; --j) { if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } } } G->n = n; G->e = e; }
void DispAdj(AdjGraph* G) { ArcNode* p; for (int i = 0; i < G->n; ++i) { p = G->adjlist[i].firstarc; cout << i << " :"; while (p != NULL) { cout << p->adjvex << "[" << p->weight << "]->"; p = p->nextarc; } cout << endl; } }
void DestroyAdj(AdjGraph*& G) { ArcNode* pre, *p; for (int i = 0; i < G->n; ++i) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p=p->nextarc; } free(pre); } } free(G); }
void dfs(AdjGraph* G, int v) { ArcNode* p; visted[v] = true; cout << v << " -> "; p = G->adjlist[v].firstarc; while (p != NULL) { if (!visted[p->adjvex]) { dfs(G, p->adjvex); } p = p->nextarc; } }
int main () { int A[][MaxV] = { {0,2,1}, {2,0,3}, {1,3,0} }; AdjGraph* G; CreateAdj(G, A, 3, 3); DispAdj(G); dfs(G, 1); DestroyAdj(G); }
|
利用链式向前星
核心代码
链式向前星的代码没有普通链表实现简单。可利用普通链表实现作参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| bool visted[MaxV];
void dfs(AdjGraph* G, int v) { ArcNode* p; visted[v] = true; cout << v << " -> "; p = G->adjlist[v].firstarc; while (p != NULL) { if (!visted[p->adjvex]) { dfs(G, p->adjvex); } p = p->nextarc; } }
|
所以,得出链式向前星遍历代码为:
1 2 3 4 5 6 7 8 9 10 11
| bool st[N];
void dfs(int i) { st[i] = true; cout << i << " -> "; for (int j = h[i]; j != -1; j = ne[j]) { if (!st[e[j]]) { dfs(e[j]); } } }
|
整体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream>
using namespace std;
const int N = 1000, M = N * 2;
int h[N], e[M], ne[M], idx; bool st[N];
void init() { idx = 0; memset(h, -1, sizeof(h)); }
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void disp() { for (int i = 0; i < N; ++i) { if (h[i] != -1){ cout << i << ":"; for(int j = h[i]; j != -1; j = ne[j]) cout << e[j] << " idx:(" << j << ")-> "; cout << endl; } } }
void dfs(int i) { st[i] = true; cout << i << " -> "; for (int j = h[i]; j != -1; j = ne[j]) { if (!st[e[j]]) { dfs(e[j]); } } }
int main () { init(); add(1,2); add(3,4); add(2,3); add(2,1); add(4,6); add(6,1); add(6,2); disp(); dfs(2); }
|