数据结构 用DFS BFS实现图的遍历
邻接表为存储结构 实现联通无向图的深度优先遍历和广度优先遍历
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100
#define INFINITY 65535
#define MAX_VERTEX_NUM 100
typedef struct QNode {
char data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &q) {
q.front = q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!q.front)
exit(-1);
q.front->next = NULL;
return 0;
}
int EnQueue(LinkQueue &q, int &e) {
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
exit(-1);
p->data = e;
p->next = NULL;
q.rear->next = p;
q.rear = p;
return 0;
}
int DeQueue(LinkQueue &q, int &e) {
if (q.front == q.rear)
return -1;
QNode *p = q.front->next;
e = p->data;
q.front->next = p->next;
if (q.rear = p)
q.rear = q.front;
free(p);
return 0;
}
int QueueEmpty(LinkQueue q) {
if (q.front == q.rear)
return 0;
else return -1;
}
typedef char VertexType;
typedef int EdgeType;
typedef struct EdgeNode {
int adjvex;
EdgeType weight;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode {
VertexType data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct {
AdjList adjList;
int numVertexes, numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *g) {
int i, j, k;
EdgeNode *e;
printf("输入顶点数和边数:\n");
scanf_s("%d%d", &g->numVertexes, &g->numEdges);
printf("输入顶点信息\n");
for (i = 0;i < g->numVertexes;i++) {
getchar();
scanf_s("%c", &g->adjList[i].data);
g->adjList[i].firstedge = NULL;
}
for (k = 0;k < g->numEdges;k++) {
printf("输入边(vi,vj)上的顶点序号:\n");
scanf_s("%d%d", &i, &j);
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = j;
e->next = g->adjList[i].firstedge;
g->adjList[i].firstedge = e;
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = i;
e->next = g->adjList[j].firstedge;
g->adjList[j].firstedge = e;
}
}
#define MAX 100
typedef int Boolean;
Boolean visited[MAX];
void DFS(GraphAdjList *GL, int i) {
EdgeNode *p;
visited[i] = true;
printf("%c\n", GL->adjList[i].data);
p = GL->adjList[i].firstedge;
while (p) {
if (!visited[p->adjvex])
DFS(GL, p->adjvex);
p = p->next;
}
}
void DFSTraverse(GraphAdjList *GL) {
int i;
for (i = 0;i < GL->numVertexes;i++) {
visited[i] = false;
}
for (i = 0;i < GL->numVertexes;i++)
if (!visited[i])
DFS(GL, i);
}
struct stack {
int a[100];
int top;
};
void push(stack *s, int data) {
s->top++;
s->a[s->top] = data;
}
int pop(stack *s) {
return s->a[s->top--];
}
int getTop(stack *s) {
return s->a[s->top];
}
int visit[10];
void DFS_stack(GraphAdjList *GL,int start) {
stack *s = (stack *)malloc(sizeof(stack));
EdgeNode *p;
s->top = -1;
printf("%c\n",GL->adjList[start]);
visit[start] = 1;
push(s, start);
while (s->top != -1) {
int tem = getTop(s);
int i;
p = GL->adjList[tem].firstedge;
while (p != NULL && visit[p->adjvex] == 1)
p = p->next;
if (p == NULL)
s->top--;
else {
printf("%c\n",GL->adjList[p->adjvex]);
visit[p->adjvex] = 1;
push(s, p->adjvex);
}
}
free(s);
}
void BFSTraverse(GraphAdjList *GL) {
int i;
EdgeNode *p;
LinkQueue Q;
for (i = 0;i < GL->numVertexes;i++) {
visited[i] = false;
InitQueue(Q);
for (i = 0;i < GL->numVertexes;i++) {
if (!visited[i]) {
visited[i] = true;
printf("%c\n", GL->adjList[i].data);
EnQueue(Q, i);
while (!QueueEmpty(Q)) {
DeQueue(Q, i);
p = GL->adjList[i].firstedge;
while (p) {
if (!visited[p->adjvex]) {
visited[p->adjvex] = true;
printf("%c\n", GL->adjList[p->adjvex].data);
EnQueue(Q, p->adjvex);
}
p = p->next;
}
}
}
}
}
}
int main() {
GraphAdjList *g;
g = (GraphAdjList *)malloc(sizeof(GraphAdjList));
CreateALGraph(g);
DFS_stack(g, 0);
}
![结果截图:](https://i-blog.csdnimg.cn/blog_migrate/fce09214279af6b937e043d574b61606.png)