上传文件至 ''

This commit is contained in:
xtaodada 2022-05-11 13:33:37 +00:00
parent b3d63b8b73
commit 14fc2cd9cc
2 changed files with 279 additions and 0 deletions

130
ex5_1.c Normal file
View File

@ -0,0 +1,130 @@
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 100
int visited[MAX];
//邻接矩阵
typedef struct graph
{
int n; //顶点数
int e; //边数
char vexs[MAX]; //顶点数组
int edges[MAX][MAX]; //边的领接矩阵
}graph,*MGraph;
void CreateMGraph(MGraph G){
char ch1,ch2;
printf("请输入顶点数:");
scanf("%d", &G->n);
printf("请输入边数:");
scanf("%d", &G->e);
printf("\n请输入顶点信息:\n");
for(int i=0; i<G->n; i++){
getchar();
printf("输入第 %d 个顶点:", i+1);
scanf("%c", &(G->vexs[i]));
}
for(int i=0; i<G->n; i++){
for(int j=0; j<G->n; j++){
G->edges[i][j]=0;
}
}
for(int i=0; i<G->e; i++){
getchar();
printf("建立第 %d 条边格式顶点1,顶点2", i+1);
scanf("%c,%c",&ch1,&ch2);
for(int j=0; j<G->n; j++){
for(int k=0; k<G->n; k++){
if(ch1==G->vexs[j]&&ch2==G->vexs[k]){
G->edges[j][k]=1;
G->edges[k][j]=1; // 无向
}
}
}
}
}
void DisplayMGraph(MGraph G){
printf("图的邻接矩阵为:\n");
for(int i=0; i<G->n; i++){
for(int j=0; j<G->n; j++){
printf("%5d", G->edges[i][j]);
}
printf("\n");
}
}
void clear(){
for(int i=0; i<MAX; i++){
visited[i] = 0;
}
}
void DFS(MGraph G, int v){
printf("%c", G->vexs[v]);
visited[v] = 1;
for(int i=0; i<G->n; i++){
if(G->edges[v][i]!=0 && visited[i]==0){
DFS(G, i);
}
}
}
void BFS(MGraph G, int v){
int Qu[MAX], front=0, rear=0, w;
printf("%c", G->vexs[v]);
visited[v]=1;
rear = (rear + 1) % MAX;
Qu[rear] = v;
while(front != rear){
front = (front + 1) % MAX;
w = Qu[front];
for(int i=0; i<G->n; i++){
if(G->edges[w][i]!=0 && visited[i]==0){
printf("%c", G->vexs[i]);
visited[i]=1;
rear = (rear + 1) % MAX;
Qu[rear] = i;
}
}
}
}
int main(){
int choice;
graph G;
for(;;){
printf("\t\t1.建立图的邻接矩阵\n");
printf("\t\t2.输出图的邻接矩阵\n");
printf("\t\t3.邻接矩阵的深度遍历\n");
printf("\t\t4.邻接矩阵的广度遍历\n");
printf("\t\t0.退出\n");
printf("请选择:");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
CreateMGraph(&G);
break;
case 2:
DisplayMGraph(&G);
break;
case 3:
clear();
printf("深度遍历结果如下:");
DFS(&G, 0);
printf("\n");
break;
case 4:
clear();
printf("广度遍历结果如下:");
BFS(&G, 0);
printf("\n");
break;
default:
break;
}
printf("请按任意键继续...");
getch();
system("cls");
}
printf("感谢使用,再见\n");
return 0;
}

149
ex5_1_1.c Normal file
View File

@ -0,0 +1,149 @@
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 100
int visited[MAX];
//邻接表
typedef struct node{ //边表
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vexnode{ //顶点表
char data;
EdgeNode *firstedge;
}VHeadNode;
typedef struct{
int n;
int e;
VHeadNode adjlist[MAX]; //顶点表数组
}adjlist,*AdjList;
void CreateAGraph(AdjList G){
char ch1,ch2;
EdgeNode *p;
printf("请输入顶点数:");
scanf("%d", &G->n);
printf("请输入边数:");
scanf("%d", &G->e);
printf("\n请输入顶点信息:\n");
for(int i=0; i<G->n; i++) {
getchar();
printf("输入第 %d 个顶点:", i+1);
scanf("%c", &(G->adjlist[i].data));
G->adjlist[i].firstedge=NULL;
}
for(int i=0; i<G->e; i++){
getchar();
printf("建立第 %d 条边格式顶点1,顶点2", i+1);
scanf("%c,%c",&ch1,&ch2);
for(int j=0; j<G->n; j++){
for(int k=0; k<G->n; k++){
if(ch1==G->adjlist[j].data && ch2==G->adjlist[k].data){
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex = k;
p->next = G->adjlist[j].firstedge;
G->adjlist[j].firstedge = p;
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex = j;
p->next = G->adjlist[k].firstedge;
G->adjlist[k].firstedge = p;
}
}
}
}
}
void DisplayAGraph(AdjList G){
EdgeNode *p;
printf("图的邻接表为:\n");
for(int i=0; i<G->n; i++){
printf("%2d [%c]", i, G->adjlist[i].data);
p = G->adjlist[i].firstedge;
while(p!=NULL){
printf("-->[%d]", p->adjvex);
p=p->next;
}
printf("\n");
}
}
void clear(){
for(int i=0; i<MAX; i++){
visited[i] = 0;
}
}
void DFS(AdjList G, int v){
EdgeNode *p;
printf("(%d,%c)", v, G->adjlist[v].data);
visited[v] = 1;
p = G->adjlist[v].firstedge;
while(p!=NULL){
if(visited[p->adjvex]==0){
DFS(G, p->adjvex);
}
p=p->next;
}
}
void BFS(AdjList G, int v){
int Qu[MAX], front=0, rear=0;
EdgeNode *p;
printf("(%d,%c)", v, G->adjlist[v].data);
visited[v]=1;
rear = (rear + 1) % MAX;
Qu[rear] = v;
while(front != rear){
front = (front + 1) % MAX;
v = Qu[front];
p = G->adjlist[v].firstedge;
while(p!=NULL){
if(visited[p->adjvex]==0){
visited[p->adjvex]=1;
printf("(%d,%c)", p->adjvex, G->adjlist[p->adjvex].data);
rear = (rear + 1) % MAX;
Qu[rear]=p->adjvex;
}
p=p->next;
}
}
}
int main(){
int choice;
adjlist G;
for(;;){
printf("\t\t1.建立图的邻接表\n");
printf("\t\t2.输出图的邻接表\n");
printf("\t\t3.邻接表的深度遍历\n");
printf("\t\t4.邻接表的广度遍历\n");
printf("\t\t0.退出\n");
printf("请选择:");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
CreateAGraph(&G);
break;
case 2:
DisplayAGraph(&G);
break;
case 3:
clear();
printf("深度遍历结果如下:");
DFS(&G, 0);
printf("\n");
break;
case 4:
clear();
printf("广度遍历结果如下:");
BFS(&G, 0);
printf("\n");
break;
default:
break;
}
printf("请按任意键继续...");
getch();
system("cls");
}
printf("感谢使用,再见\n");
return 0;
}