C语言利用栈函数实现邻接表的深度优先遍历和广度优先遍历,用代码显示出来
时间: 2024-05-12 20:18:47 浏览: 107
c语言实现的邻接表连通图深度优先遍历
3星 · 编辑精心推荐
深度优先遍历代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
} ArcNode;
typedef struct {
char data;
ArcNode *firstarc;
} VNode;
typedef struct {
VNode adjlist[MAX_VERTEX_NUM];
int vexnum, arcnum;
} ALGraph;
typedef struct {
int data[MAX_VERTEX_NUM];
int top;
} Stack;
void InitStack(Stack *S) {
S->top = -1;
}
int StackEmpty(Stack *S) {
return S->top == -1;
}
void Push(Stack *S, int x) {
S->data[++(S->top)] = x;
}
int Pop(Stack *S) {
return S->data[(S->top)--];
}
void DFS(ALGraph *G, int v, int visited[]) {
printf("%c ", G->adjlist[v].data);
visited[v] = 1;
ArcNode *p = G->adjlist[v].firstarc;
while (p) {
if (!visited[p->adjvex]) {
DFS(G, p->adjvex, visited);
}
p = p->nextarc;
}
}
void DFSTraverse(ALGraph *G) {
int visited[MAX_VERTEX_NUM] = {0};
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
DFS(G, i, visited);
}
}
}
int main() {
ALGraph G;
int visited[MAX_VERTEX_NUM] = {0};
printf("请输入图的顶点数和边数:\n");
scanf("%d%d", &G.vexnum, &G.arcnum);
printf("请输入%d个顶点的值:\n", G.vexnum);
for (int i = 0; i < G.vexnum; i++) {
scanf(" %c", &(G.adjlist[i].data));
G.adjlist[i].firstarc = NULL;
}
printf("请输入%d条边的弧尾和弧头:\n", G.arcnum);
for (int i = 0; i < G.arcnum; i++) {
int u, v;
scanf("%d%d", &u, &v);
ArcNode *newArcNode = (ArcNode *)malloc(sizeof(ArcNode));
newArcNode->adjvex = v;
newArcNode->nextarc = G.adjlist[u].firstarc;
G.adjlist[u].firstarc = newArcNode;
}
printf("深度优先遍历结果为:");
DFSTraverse(&G);
printf("\n");
return 0;
}
```
广度优先遍历代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
} ArcNode;
typedef struct {
char data;
ArcNode *firstarc;
} VNode;
typedef struct {
VNode adjlist[MAX_VERTEX_NUM];
int vexnum, arcnum;
} ALGraph;
typedef struct {
int data[MAX_VERTEX_NUM];
int front, rear;
} Queue;
void InitQueue(Queue *Q) {
Q->front = Q->rear = -1;
}
int QueueEmpty(Queue *Q) {
return Q->front == Q->rear;
}
void EnQueue(Queue *Q, int x) {
Q->data[++(Q->rear)] = x;
}
int DeQueue(Queue *Q) {
return Q->data[++(Q->front)];
}
void BFS(ALGraph *G, int v, int visited[]) {
Queue Q;
InitQueue(&Q);
printf("%c ", G->adjlist[v].data);
visited[v] = 1;
EnQueue(&Q, v);
while (!QueueEmpty(&Q)) {
int u = DeQueue(&Q);
ArcNode *p = G->adjlist[u].firstarc;
while (p) {
int w = p->adjvex;
if (!visited[w]) {
printf("%c ", G->adjlist[w].data);
visited[w] = 1;
EnQueue(&Q, w);
}
p = p->nextarc;
}
}
}
void BFSTraverse(ALGraph *G) {
int visited[MAX_VERTEX_NUM] = {0};
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
BFS(G, i, visited);
}
}
}
int main() {
ALGraph G;
printf("请输入图的顶点数和边数:\n");
scanf("%d%d", &G.vexnum, &G.arcnum);
printf("请输入%d个顶点的值:\n", G.vexnum);
for (int i = 0; i < G.vexnum; i++) {
scanf(" %c", &(G.adjlist[i].data));
G.adjlist[i].firstarc = NULL;
}
printf("请输入%d条边的弧尾和弧头:\n", G.arcnum);
for (int i = 0; i < G.arcnum; i++) {
int u, v;
scanf("%d%d", &u, &v);
ArcNode *newArcNode = (ArcNode *)malloc(sizeof(ArcNode));
newArcNode->adjvex = v;
newArcNode->nextarc = G.adjlist[u].firstarc;
G.adjlist[u].firstarc = newArcNode;
}
printf("广度优先遍历结果为:");
BFSTraverse(&G);
printf("\n");
return 0;
}
```
阅读全文