成长路上的小程序之——图的邻接表DFS、BFS

2024-06-14 03:58
文章标签 程序 bfs dfs 成长 邻接 路上

本文主要是介绍成长路上的小程序之——图的邻接表DFS、BFS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第八次数据结构上机内容:使用图的邻接表存储结构完成图的DFS、BFS遍历

本来提前就用邻接矩阵写完了,但昨晚一个贱人突然告诉我老师要求用邻接表写。。。于是加班加点,终于在今早搞出来了!

上机课的时候因为没带书,所以完全是自己敲的,期间被一个无限循环困住了!!!

在邻接链表插入新节点过程中,为p申请新空间,插入后就释放空间

刘文斌大神的帮助下,我理解了开辟、释放空间这个概念。

我在创建图的过程中,更改了p的指向,并且最后释放p时,p为空指针,所以没有错误。

释放空间时,要确保能够找到之前创建的所有空间并释放!!!!

应该在使用完成后释放空间!!

以后再申请新空间时,无论如何也要放在最后释放空间,这样不容易出错。

代码中还有一个问题就是在FirstAdjVex()NextAdjVex()函数中,没有释放空间,因为试过只要释放空间就无法使DFS函数结束。

解决方法是在创建空间的时候,另设一个标志指针指向该位置,使用完成后释放标志指针所指空间即可

再次感谢刘文斌大神

得意

代码如下:


#define MAX 20typedef char VertexType;typedef struct ArcNode{int adjvex;int w;struct ArcNode *nextarc;
}ArcNode;typedef struct VNode{VertexType data;ArcNode *firstarc;
}VNode,AdjList[MAX];typedef struct{AdjList vertices;   int vexnum;int arcnum;int kind;
}ALGraph;int Locate(ALGraph G,char x)
{for(int i=0; i<G.vexnum; i++)if(G.vertices[i].data==x) return i;return -1; 
} Status CreateDG(ALGraph &G)//创建有向图 
{char v1,v2; int  j; ArcNode *p;printf("\nplease enter the vexnum and arcnum:\n");scanf("%d%d",&G.vexnum,&G.arcnum);getchar();printf("\nplease enter the vexs in order:\n");for(int i=0; i<G.vexnum; i++){scanf("%c",&G.vertices[i].data);G.vertices[i].firstarc=NULL;} printf("\nplease enter edge:\n");for(int i=0; i<G.arcnum; i++){getchar(); scanf("%c %c",&v1,&v2); j=Locate(G,v1); //定位v1位置  p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=Locate(G,v2);p->w=0;p->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p;//不可以在这里释放空指针!!!} printf("\nHere are the ALGraph:\n"); for(int i=0; i<G.vexnum; i++){printf("%c",G.vertices[i].data);p=(ArcNode *)malloc(sizeof(ArcNode)); p=G.vertices[i].firstarc;while(p)     {printf(" -> %d",p->adjvex);p=p->nextarc;    }printf("\n\n");} return OK;
}Status CreateDN(ALGraph &G)//创建有向网 
{char v1,v2; int  j,w; ArcNode *p;printf("\nplease enter the vexnum and arcnum:\n");scanf("%d%d",&G.vexnum,&G.arcnum);getchar();printf("\nplease enter the vexs in order:\n");for(int i=0; i<G.vexnum; i++){scanf("%c",&G.vertices[i].data);G.vertices[i].firstarc=NULL;} printf("\nplease enter edge:\n");for(int i=0; i<G.arcnum; i++){getchar(); scanf("%c %c %d",&v1,&v2,&w); j=Locate(G,v1); //定位v1位置  p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=Locate(G,v2);p->w=w;p->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p;//不可以在这里释放空指针!!! }printf("\nHere are the ALGraph:\n"); for(int i=0; i<G.vexnum; i++){printf("%c",G.vertices[i].data);p=(ArcNode *)malloc(sizeof(ArcNode)); p=G.vertices[i].firstarc;while(p)     {printf("->[ %d weight:%d ]",p->adjvex,p->w);p=p->nextarc;    }printf("\n\n");}return OK;
}Status CreateUDG(ALGraph &G)//创建无向图 
{char v1,v2; int  j,k; ArcNode *p,*q,*flag;printf("\nplease enter the vexnum and arcnum:\n");scanf("%d%d",&G.vexnum,&G.arcnum);getchar();printf("\nplease enter the vexs in order:\n");for(int i=0; i<G.vexnum; i++){scanf("%c",&G.vertices[i].data);G.vertices[i].firstarc=NULL;} printf("\nplease enter edge:\n");for(int i=0; i<G.arcnum; i++){getchar(); scanf("%c %c",&v1,&v2); j=Locate(G,v1); //定位v1位置  k=Locate(G,v2);p=(ArcNode *)malloc(sizeof(ArcNode)); q=(ArcNode *)malloc(sizeof(ArcNode));p->adjvex=k;p->w=0;p->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p;q->adjvex=j;q->w=0;q->nextarc=G.vertices[k].firstarc;G.vertices[k].firstarc=q;//不可以在这里释放空指针!!!} printf("\nHere are the ALGraph:\n"); for(int i=0; i<G.vexnum; i++){printf("%c",G.vertices[i].data);p=G.vertices[i].firstarc;while(p)     {printf(" -> %d",p->adjvex);p=p->nextarc;    }printf("\n\n");}return OK;
}Status CreateUDN(ALGraph &G)//创建无向网 
{char v1,v2; int  j,w,k; ArcNode *p,*q;printf("\nplease enter the vexnum and arcnum:\n");scanf("%d%d",&G.vexnum,&G.arcnum);getchar();printf("\nplease enter the vexs in order:\n");for(int i=0; i<G.vexnum; i++){scanf("%c",&G.vertices[i].data);G.vertices[i].firstarc=NULL;} printf("\nplease enter edge:\n");for(int i=0; i<G.arcnum; i++){getchar(); scanf("%c %c %d",&v1,&v2,&w); j=Locate(G,v1); //定位v1位置  k=Locate(G,v2);p=(ArcNode *)malloc(sizeof(ArcNode)); q=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=k;p->w=w;p->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p;//不可以在这里释放空指针!!! q->adjvex=j;q->w=w;q->nextarc=G.vertices[k].firstarc;G.vertices[k].firstarc=q;}printf("\nHere are the ALGraph:\n"); for(int i=0; i<G.vexnum; i++){printf("%c",G.vertices[i].data);p=(ArcNode *)malloc(sizeof(ArcNode)); p=G.vertices[i].firstarc;while(p)     {printf("->[ %d weight:%d ]",p->adjvex,p->w);p=p->nextarc;    }printf("\n\n");}return OK;
}void CreateMenu(ALGraph &G)
{printf("please enter the Graph's kind:\n");printf("\n0 present DG(有向图)\n");printf("\n1 present DN(有向网)\n");printf("\n2 present UDG(无向图)\n");printf("\n3 present UDN(无向网)\n");char ch=getchar();switch(ch){case '0':CreateDG(G);break;    case '1':CreateDN(G);break;    case '2':CreateUDG(G);break;    case '3':CreateUDN(G);break;    }return ;
} ArcNode *p;
bool visited[MAX];int FirstAdjVex(ALGraph G, int v)
{p=(ArcNode *)malloc(sizeof(ArcNode)); p=G.vertices[v].firstarc;if(p)return p->adjvex;else return -1;
}int NextAdjVex(ALGraph G, int v, int w)
{p=(ArcNode *)malloc(sizeof(ArcNode)); p=G.vertices[v].firstarc;for(int i=0; i<MAX; i++){if(p->adjvex==w){ if(p->nextarc){ return p->nextarc->adjvex;} else return -1;}elsep=p->nextarc;}
}void DFS(ALGraph G, int v)
{visited[v]=true;printf("%c   ",G.vertices[v].data);for(int w=FirstAdjVex(G,v); w>=0; w=NextAdjVex(G,v,w)){if(!visited[w])DFS(G,w);}return ;
}void DFSTraverse(ALGraph G)
{printf("\n\n\nHere are the results of DFSTraverse:\n\n");int v;for(v=0; v<G.vexnum; v++)visited[v]=false;for(v=0; v<G.vexnum; v++)if(!visited[v])DFS(G,v);printf("\n");return ;
}//----------队列 void BFSTraverse(ALGraph G)
{int que[MAX]={-1}; int u;int rear=0,front=0;printf("\n\n\nHere are the results of BFSTraverse:\n\n");for(int v=0; v<G.vexnum; v++)visited[v]=false;for(int v=0; v<G.vexnum; v++)if(!visited[v]){visited[v]=true;printf("%c   ",G.vertices[v].data);que[++rear]=v;while(rear!=front){u=que[++front];for(int w=FirstAdjVex(G,u); w>=0; w=NextAdjVex(G,u,w))if(!visited[w]){visited[w]=true;printf("%c   ",G.vertices[w].data);que[++rear]=w;   }      } }printf("\n");
}void menu(ALGraph G)
{printf("\n\npleae choice the traverse:");printf("\n\n1.DFS\n\n2.BFS\n\n");getchar();char ch=getchar();if(ch=='1')return DFSTraverse(G);else if(ch=='2')return BFSTraverse(G);
} 



main函数:

#include <cstdlib>
#include <iostream>
#include "constant.h"
#include "graph.h"using namespace std;int main(int argc, char *argv[])
{ArcNode *p,*flag; ALGraph G;CreateMenu(G);menu(G);for(int i=0; i<G.vexnum; i++){p=G.vertices[i].firstarc;while(p)     {flag=p->nextarc;free(p);p=flag;}}system("PAUSE");return EXIT_SUCCESS;
}


这篇关于成长路上的小程序之——图的邻接表DFS、BFS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1059306

相关文章

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Java使用WebView实现桌面程序的技术指南

《Java使用WebView实现桌面程序的技术指南》在现代软件开发中,许多应用需要在桌面程序中嵌入Web页面,例如,你可能需要在Java桌面应用中嵌入一部分Web前端,或者加载一个HTML5界面以增强... 目录1、简述2、WebView 特点3、搭建 WebView 示例3.1 添加 JavaFX 依赖3

防止SpringBoot程序崩溃的几种方式汇总

《防止SpringBoot程序崩溃的几种方式汇总》本文总结了8种防止SpringBoot程序崩溃的方法,包括全局异常处理、try-catch、断路器、资源限制、监控、优雅停机、健康检查和数据库连接池配... 目录1. 全局异常处理2. 使用 try-catch 捕获异常3. 使用断路器4. 设置最大内存和线

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.