数据结构——图的基本操作

#include <stdio.h>
#include <stdlib.h>///*图的结构定义============*///
#define MAX 500
typedef struct arcnode
{int adjvex;struct arcnode *next;int value;
} arcnode;
typedef struct vnode
{int date;arcnode *firstac;
} vnode;
typedef struct
{vnode vlist[MAX];int vexnum,arcnum;int kind;
} algraph;
///*-------------------------*//////*队列链式的实现==============*///
typedef int elem;
typedef struct queueNode{elem data;struct queueNode* next;
}queueNode;
queueNode* initQueue(queueNode *head)
{head = (queueNode*)malloc(sizeof(queueNode));if(!head) exit(0);head->next = NULL;return head;
}
queueNode* enQueue(queueNode* head,elem data)
{queueNode* p = (queueNode*)malloc(sizeof(queueNode));if(!p) exit(0);p->data = data;p->next = NULL;queueNode* pr = head;while(pr->next){pr = pr->next;}pr->next = p;return head;
}
elem deQueue(queueNode* head)
{if(!head->next) return -1;queueNode* p = head->next;elem temp = p->data;head->next = p->next;free(p);return temp;
}
int empty(queueNode* head)
{return head->next ? 0 : 1;
}
///*========================*/////图的创建 noProblem
void creGraph(algraph* al)
{int i,j;scanf("%d%d%d",&al->kind,&al->vexnum,&al->arcnum);for(i = 0; i < al->vexnum; i ++){scanf("%d",&al->vlist[i].date);al->vlist[i].firstac = NULL;}for(j = 0; j < al->arcnum; j ++){int a,b;scanf("%d %d",&a,&b);arcnode* p = (arcnode*)malloc(sizeof(arcnode));p->next = NULL;for(i = 0; i < al->vexnum; i ++){if(al->vlist[i].date == b){p->adjvex = i;break;}}for(i = 0; i < al->vexnum; i ++){if(al->vlist[i].date == a){arcnode* pr = al->vlist[i].firstac;if(!pr){al->vlist[i].firstac = p;break;}while(pr->next) pr = pr->next;pr->next = p;break;}}}printf("UDG has been created successfully.\n");
}
//输出邻接表  noProblem
void printList(algraph *al)
{putchar('\n');int i;for(i = 0;i < al->vexnum;i ++){printf("jiedian:%d ",al->vlist[i].date);arcnode *p = al->vlist[i].firstac;while(p){printf("next:%d value:%d ",p->adjvex,p->value);p = p->next;}putchar('\n');}
}
//销毁图
void desGraph(algraph* al)
{int i;for(i = 0;i < al->vexnum;i ++){arcnode* pr = al->vlist[i].firstac;arcnode* p =NULL;while(pr){p = pr;pr = pr->next;free(p);}free(&al->vlist[i].date);}al->arcnum = 0;al->vexnum = 0;al->kind = -1;
}
//查找节点图中的下标  noProblem
int local(algraph* al,int ch)
{int i;for(i = 0; i < al->vexnum; i ++){if(al->vlist[i].date == ch)return i;}printf("顶点输入错误!!!");return -1;
}
//返回特定节点的值 noProblem
int GetVex(algraph* al,int v)
{int i;for(i = 0;i < al->vexnum;i ++){arcnode* p = al->vlist[i].firstac;while(p){if(p->adjvex == v){return p->value;}p = p->next;}}return -1;
}
//对图中的节点v赋值 noProblem
void PutVexing(algraph* al,int v,int value)
{int i;for(i = 0;i < al->vexnum;i ++){arcnode *p = al->vlist[i].firstac;while(p){if(p->adjvex == v){p->value = value;}p = p->next;}}
}
//求图中顶点v的第一个邻接顶点 noProblem
void FirdtAdjVexf(algraph* al,int v)
{int i;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){printf("FirstNext:%d",al->vlist[i].firstac->adjvex);}}
}
//在图中新增添节点v
void InsertVex(algraph* al,int v)
{al->vlist[al->vexnum].date = v;al->vlist[al->vexnum].firstac = NULL;al->vexnum ++;
}
//在图中增添弧,如果是无向的,还应添加对称点 noProblem
//1.有向图 2.无向图 3.有向网 4.无向网
void InsertArc(algraph* al,int v,int w)
{int i ;arcnode* p = (arcnode*)malloc(sizeof(arcnode));if(!p) exit(0);p->adjvex = w;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){p->next = al->vlist[i].firstac;al->vlist[i].firstac = p;al->arcnum ++;break;}}if(al->kind == 2 || al->kind == 4){arcnode* pr = (arcnode*)malloc(sizeof(arcnode));if(!pr) exit(0);pr->adjvex = v;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == w){pr->next = al->vlist[i].firstac;al->vlist[i].firstac = pr;al->arcnum ++;break;}}}
}
//删除顶点v即相关的弧  noProblem
void DeleteVex(algraph* al,int v)
{int i,j;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){for(j = i;j < al->vexnum - 1;j ++){al->vlist[j] = al->vlist[j + 1];}al->vexnum --;break;}}for(i = 0;i < al->vexnum;i ++){arcnode* pr = al->vlist[i].firstac;arcnode* p = al->vlist[i].firstac;while(pr){if(pr->adjvex == v){if(p == pr)//第一个节点符合条件时{al->vlist[i].firstac = pr->next;free(pr);al->arcnum --;break;}p->next = pr->next;free(pr);al->arcnum --;break;}p = pr;pr = pr->next;}}
}
//删除弧,如果是无向的,还要删除对称弧  noProblem
void DeleteArc(algraph* al,int v,int w)
{int i;int vIndex,wIndex;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v)vIndex = i;if(al->vlist[i].date == w)wIndex = i;}arcnode *p = al->vlist[vIndex].firstac;arcnode *pr = al->vlist[vIndex].firstac;while(p){if(p->adjvex == wIndex){if(p == pr){al->vlist[vIndex].firstac = p->next;free(p);al->arcnum --;break;}pr->next = p->next;free(p);al->arcnum --;break;}pr = p;p = p->next;}if(al->kind == 2 || al->kind == 4){p = al->vlist[wIndex].firstac;pr = al->vlist[wIndex].firstac;while(p){if(p->adjvex == vIndex){if(p == pr){al->vlist[wIndex].firstac = p->next;free(p);al->arcnum --;break;}pr->next = p->next;free(p);al->arcnum --;break;}pr = p;p = p->next;}}
}
//深度优先遍历 noProblem
void dnf(algraph* al,int i);
int visited[MAX];
void dnfTrave(algraph* al)
{int i;for(i = 0; i < al->vexnum; i ++)visited[i] = 0;for(i = 0; i < al->vexnum; i ++)if(!visited[i]) dnf(al,i);
}
void dnf(algraph* al,int i)
{visited[i] = 1;printf("%d ",al->vlist[i].date);arcnode *p = al->vlist[i].firstac;while(p){if(!visited[p->adjvex])dnf(al,p->adjvex);p = p->next;}
}
//广度优先遍历 noProblem
void BFSTraverse(algraph* al)
{int i;for(i = 0;i < al->vexnum;i ++)visited[i] = 0;queueNode* head = NULL;head = initQueue(head);for(i = 0;i < al->vexnum;i ++){if(!visited[i]){visited[i] = 1;printf("%d ",al->vlist[i].date);head = enQueue(head,i);while(!empty(head)){arcnode *p = al->vlist[deQueue(head)].firstac;while(p){if(!visited[p->adjvex]){visited[p->adjvex] = 1;printf("%d ",al->vlist[p->adjvex].date);head = enQueue(head,p->adjvex);}p = p->next;}}}}}
//求顶点v的度 noProblem
int FindDegree(algraph* al,int v)
{int count = 0;int i = 0;int vIndex;arcnode *p = NULL;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){vIndex = i;break;}}for(i = 0;i < al->vexnum;i ++){p = al->vlist[i].firstac;while(p){if(p->adjvex == vIndex){count ++;break;}p = p->next;}}p = al->vlist[vIndex].firstac;while(p){count ++;p = p->next;}return count;
}
//计算出度
int FindOutDegree(algraph* al,int v)
{int count = 0;int i = 0;int vIndex;arcnode *p = NULL;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){vIndex = i;break;}}p = al->vlist[vIndex].firstac;while(p){count ++;p = p->next;}return count;
}
//求有向图中顶点v的入度
int FindInDegree(algraph* al,int v)
{int count = 0;int i = 0;int vIndex = -1;arcnode *p = NULL;for(i = 0;i < al->vexnum;i ++){if(al->vlist[i].date == v){vIndex = i;break;}}if(vIndex == -1) printf("节点入度计算错误,没有该节点");for(i = 0;i < al->vexnum;i ++){p = al->vlist[i].firstac;while(p){if(p->adjvex == vIndex){count ++;break;}p = p->next;}}return count;
}
int main()
{algraph al;creGraph(&al);printf("DFS traverse: ");dnfTrave(&al);putchar('\n');printf("BFS traverse: ");BFSTraverse(&al);putchar('\n');printf("The degree of each vertex: ");int i = 0;for(i = 0;i < al.vexnum;i ++){printf("%d: ",al.vlist[i].date);printf("%d",FindOutDegree(&al,al.vlist[i].date)+FindInDegree(&al,al.vlist[i].date));if(i != al.vexnum-1) printf(", ");}return 0;
}
//测试数据
/*
3 8 9
1 2 3 4 5 6 7 8
1 2
1 3
2 4
2 5
4 8
5 8
3 6
3 7
6 7*/

数据结构——图的基本操作相关推荐

  1. 数据结构 - 图的基本操作 深度和广度遍历

    ##代码实现 ###main.cpp(主函数) #include <iostream> #include "CMap.h" using namespace std;/* ...

  2. 图的基本操作实现(数据结构实验)

    实验项目六 图结构基本操作的实现 课程名称:数据结构 实验项目名称:图结构基本操作的实现 实验目的: 1.掌握图的基本操作-遍历. 实验要求: 1.    分别用DFS和BFS的方法实现一个无向图的遍 ...

  3. python数据结构实验目的_图的基本操作实现(数据结构实验)

    课程名称:数据结构 实验项目名称:图结构基本操作的实现 实验目的: 1.掌握图的基本操作-遍历. 实验要求: 1. 分别用DFS和BFS的方法实现一个无向图的遍历. 实验过程: 1. 创建一个图(可用 ...

  4. 图的基本操作(数据结构实验报告)

    数据结构 之 图的基本操作 实验报告 文章目录 数据结构 之 图的基本操作 实验报告 一. 实验目的 二. 实验内容 三.实验要求 四. 代码 五.运行结果 一. 实验目的 ​ (1)理解图的基本术语 ...

  5. 算法与数据结构--图的实现、基本操作及应用

    #include<iostream> #include<queue> #include<stack> using namespace std;#define INF ...

  6. 数据结构-图-邻接矩阵-试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc-icoding

    邻接矩阵 试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc,相关定义如下: typedef int VertexType;type ...

  7. 数据结构之图的基本操作

    图的基本操作 基本操作: 判断边是否存在: 列出与某节点相邻的边: 在图中插入一个顶点: 在图中删除一个顶点: 在图中添加一条边: 在图中删除一个边: 查找某顶点的第一个邻接点: 查找某边的权值: 查 ...

  8. C++数据结构实验--图的基本操作

    实验四 图的基本操作 1.实验内容与要求 理解邻接矩阵和邻接表的概念 任选一种存储方式实现以下操作:图的构造.深度优先遍历以及广度优先遍历 选作:最小生成树和最短路径. 2.实验环境 硬件环境:计算机 ...

  9. 【数据结构】图的基本操作

    一.问题描述 分别以邻接矩阵和邻接表作为存储结构,实现以下图的基本操作: 增加一个新结点v,Insert(G,v): 删除顶点v及其相关的边,Delete(G,v): 增加一条边<v,w> ...

  10. 【数据结构(C语言)】数据结构-图

    图 文章目录 图 一.基本概念 1.图的定义 2.约定符号 3.分类 4.子图 5.路 6.其他术语 7.ADT 二.存储结构 1.邻接矩阵(数组) 2.邻接表 三.基本算法 1.遍历 2.求无向图的 ...

最新文章

  1. 利用ZYNQ SOC快速打开算法验证通路(5)——system generator算法IP导入IP integrator
  2. cesium鼠标控制键盘_用 Python 控制 鼠标和键盘,花式操作
  3. 手把手教你使用Pandas读取结构化数据
  4. VScode配置CMD本地运行环境(2.0)
  5. 自定义控件之绘图篇(一):概述及基本几何图形绘制
  6. 最简洁的方式,实现web端百度地图一键定位导航
  7. python如何生成随机数_python如何生成随机数
  8. 堆优化的Dijkstra
  9. cad详图怎么画_CAD施工图怎样能画的快?老师傅教你20个制图诀窍,远超他人!
  10. ajax测试报错Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Co
  11. Codeforces 1023G:Pisces(最长反链)
  12. 移动端图形架构之PowerVR of Imagination Tile-based rendering
  13. 3G杀手应用还是全方位轰炸?
  14. windows系统查看USB设备序列号方法汇总
  15. python 英语翻译_用Python将日语翻译成英语
  16. 百度地图js 定位并获得精确的地址信息
  17. mysql获取汉字拼音首字母_MySQL数据库获取汉字拼音的首字母函数
  18. bilibili正在缓冲却无法播放
  19. oracle开窗函数有哪些,oracle分析函数理解(开窗函数)
  20. 批量给没有扩展名的文件追加新的扩展名从而修改文件名

热门文章

  1. 【Pytorch编程】Pytorch-Ignite v0.4.8的安装以及简单使用
  2. VMware12 pro 安装win7时operating system not found
  3. SpringMVC面试题及答案
  4. MATLAB 产生带宽信号
  5. mysql中round函数使用
  6. 大数据平台及挖掘调研
  7. 如何批量将 Word 转换为 jpeg、png、bmp 图片
  8. 「缠师课后回复精选」第15课:没有趋势,没有背驰。
  9. android 视频 缩略图,安卓平台生成视频缩略图的几种方法
  10. 飞桨day-03 人脸关键点检测实践作业