图操作
来源:互联网 发布:php protobuf 编辑:程序博客网 时间:2024/06/08 10:04
程序代码:
#include"stdio.h"#include"stdlib.h"#include<iostream>using namespace std;typedef struct node{//定义链表中的一个结点 int adj; struct node*next;}arcnode;typedef struct vnode{//定义顶点类型 int data; arcnode*firstarc;}vnode;int visited[5] = { 0, 0, 0, 0, 0 };void creatgraph(vnode g[], int n)//创建具有n个结点的图{ int a; for (int i = 0; i < n; i++)//创造n个顶点 { cin >> a; g[i].data = a; g[i].firstarc = NULL; } int e; arcnode *p=NULL, *q=NULL; for (int i = 0; i < n; i++)//分别创造每个顶点的边 { printf("creat the edges of the %d vertex\n", i); cin >> e; while (e != -1) { p = (arcnode*)malloc(sizeof(arcnode)); p->adj = e; p->next = NULL; if (g[i].firstarc == NULL)//如果是第一条边 g[i].firstarc = p; else//当不是第一条边的情况 q->next = p; q = p; cin >> e; } }}int firstadj(vnode g[], int v)//返回第一邻接点在数组中的下标{ if (g[v].firstarc != NULL) return (g[v].firstarc)->adj; return -1;}int nextadj(vnode g[], int v)//返回下一邻接点在数组中的下标{ arcnode *p = g[v].firstarc; while (p != NULL) { if (visited[p->adj]) p = p->next; else return p->adj; } return -1;}void DFS(vnode g[], int v)//深度优先搜索{ printf("%d ", g[v].data); visited[v] = 1; int w; w = firstadj(g, v); while (w != -1) { if (visited[w] == 0) DFS(g, w); w = nextadj(g, v); }}int main(){ vnode g[5]; creatgraph(g,5); DFS(g, 0); system("pause"); return 0;}
代码结果:
0 0
- 图操作
- 图的基本操作
- webgis图层操作
- 图的相关操作
- 图元操作实例
- 图的操作
- 图操作 && help pro_lee
- 图的操作
- 图的遍历操作
- 图的基本操作
- 图的操作
- staruml 时序图操作
- 图的基本操作
- 图基本操作
- 图的操作 code
- 图的基本操作
- 图的相关操作
- 图层操作
- java接口、内部类、异常
- (3)Struts2-拦截器和数据校验
- Unable to execute dex设备安装app失败
- 【机房收费系统】上机
- 自定义Kafka分区器
- 图操作
- Python 调用C模块以及性能分析
- memcached 学习笔记
- Codeforces 743D-Chloe and pleasant prizes
- 悬赏预告功能需求分析
- 时间工具类
- java.lang.NoClassDefFoundError: 异常处理记录
- 整数对查找
- [哈夫曼树 堆] BZOJ 2923 [Poi1998]The lightest language