#include<stdio.h>
#include<stdlib.h>
#define Max 10
typedef int elementType;//注意这个,我为了和队列的保持一致,所以用了int
//定义图顶点相关的内容
typedef struct Graph_g {
//peek[]数组的类型可以是字符,根据自己的喜欢定义
elementType peek[Max];
int number_vertex;//定义顶点个数
int nubmer_edge;//定义边的个数
int G[Max][Max];//定义临接矩阵
//定义是否被访问的数组
bool c[Max];
}Graph,*Graph_matrax;
//对图进行初始化
Graph_matrax init() {
Graph_matrax graph= (Graph_matrax)malloc(sizeof(Graph));
int vertex;
printf("请输入顶点的个数:");
scanf("%d", &vertex);
graph->number_vertex = vertex;
graph->nubmer_edge = 0;
int i;
//对顶点的信息进行赋值
for (i = 0; i < vertex; i++)
{
printf("请输入%d个顶点的信息:",i+1);
scanf("%d", &graph->peek[i]);
}
//临接矩阵的对脚线赋值为0,其他的初始化为最大值,假设全都不连通
for (int i = 0; i < graph->number_vertex; i++) {
for (int j = 0; j < graph->number_vertex; j++)
{
if (i == j)
graph->G[i][j] = 0;
else
graph->G[i][j] = 8888;
}
}
int edge=0;
int v1, v2, w;
printf("请输入边的数目:");
scanf("%d", &edge);
for (i = 0; i < edge; i++) {
printf("请输入i,j,w,有关顶点的到顶点的边的相关信息:");
scanf("%d%d%d", &v1, &v2, &w);
graph->G[v1 - 1][v2 - 1] = w;
graph->G[v2 - 1][v1 - 1] = w;
}
return graph;
}
void display(Graph_matrax g) {
printf("输出临接矩阵:\n");
for (int i = 0; i < g->number_vertex; i++) {
for (int j = 0; j < g->number_vertex; j++)
{
printf("%8d", g->G[i][j]);
}
printf("\n");
}
}
//定义队列的目的主要为了在广度优先遍历过程中使用队列
//定义队列的相关实现
typedef struct q_node {
elementType data;
q_node *next;
}Node;
typedef struct Link_Node {
struct q_node *rear;
struct q_node *front;
} L_node;
L_node* init1() {
L_node *p = (L_node *)malloc(sizeof(L_node));
if (p == NULL) {
printf("内存满了\n");
return NULL;
}
p->rear = p->front = NULL;
printf("初始化成功\n");
return p;
}
void push_queue(L_node *p, elementType x) {
//新建个节点存储数据
Node *temp = (Node *)malloc(sizeof(Node));
if (temp == NULL)
{
printf("内存满了\n");
return;
}
temp->data = x;
temp->next = NULL;
//判断下队列是否无元素
if (p->rear == NULL) {
p->rear = temp;
p->front = temp;
}
//如果非空,就把元素插入到队尾指针的后面
p->rear->next = temp;
//要把队尾指针指向新创建的节点
p->rear = temp;
}
elementType pop_queue(L_node * p) {
Node * temp = p->front;
elementType value;
if (p->front == NULL) {
printf("队列已经空了\n");
return -1;
}
//判断下如果队列只有一个元素
if (p->front == p->rear) {
value = p->front->data;
p->front = NULL;
p->rear = NULL;
free(temp);
return value;
}
value = p->front->data;
p->front = temp->next;
free(temp);
return value;
}
void DFS(Graph_matrax g, int x) {
g->c[x-1] = true;
printf("%2d", g->peek[x - 1]);
for (int i = 0; i < g->number_vertex; i++) {
if (x - 1 != i)
if (g->G[x - 1][i] == 1)
if (g->c[i] != true)
DFS(g, i+1);
}
}
//使用队列实现广度优先遍历
void BFS(Graph_matrax g, L_node *queue, int x) {
//本来想用 g->c[]这个的,可是不行,我在这把值置为false没起作用 //故新建了d[Max]
bool d[Max];
for (int i = 0; i < g->nubmer_edge; i++)
d[i] = false;
//把该访问过的顶点放置为true
int j;
d[x - 1] = true;
printf("%2d", g->peek[x - 1]);
//把刚访问的点压入到栈中
push_queue(queue, g->peek[x - 1]);
//判断队列是否为空,如果为空,则说明广度优先遍历完成
while (queue->front!=NULL)
{
j = pop_queue(queue);
for (int i = 0; i < g->number_vertex; i++) {
if(i!=j-1)
if(g->G[j-1][i]==1)
if (d[i]!= true)
{
d[i] = true;
printf("%2d", g->peek[i]);
push_queue(queue, g->peek[i]);
}
}
}
} int main() {
Graph *g = init();
printf("%d\n", g->number_vertex);
L_node *p = init1();
display(g);
DFS(g, 1);
printf("\n");
BFS(g, p, 1);
printf("\n");
system("pause");
return 0;
}