构建图的邻接表
来源:互联网 发布:马里兰艺术学院 知乎 编辑:程序博客网 时间:2024/06/06 16:27
图的模型
示例代码
#include <iostream>using namespace std;const int topnode = 10;struct arcnode{int value;arcnode* next;};struct node{int degree;arcnode* first;};node list[topnode+1];static int nodeNum = 0;static int graphType;void setgrapth(){cout << "please input node number: ";cin >> nodeNum;for(int i = 1; i <= nodeNum; i++){list[i].degree = 0;list[i].first = NULL;}cout << "please input the graph type: 1 direct grapth 2 no direct grapth \n";cin >> graphType; int begin = 0;int end = 0;cout << "please input the side: format like these \"beginNum endNum \"\n";while(cin >> begin >> end){arcnode* tmp = list[begin].first;while( tmp != NULL && tmp->value != end ) { tmp = tmp->next; }if(tmp == NULL){tmp = new arcnode;tmp->value = end;if( NULL == list[begin].first){list[begin].first = new arcnode;list[begin].first->value = begin;list[begin].first->next = tmp;tmp->next = NULL;}else{tmp->next = list[begin].first->next;list[begin].first->next = tmp;}list[begin].degree++;if(graphType == 2){arcnode* tmp2 = new arcnode;tmp2->value = begin;if( NULL == list[end].first ){list[end].first = new arcnode;list[end].first->value = end;list[end].first->next = tmp2;tmp2->next = NULL;}else{tmp2->next = list[end].first->next;list[end].first->next = tmp2;}list[end].degree++;}}}}void printGraph(){for(int i=1; i <= nodeNum; i++){arcnode* printNode = list[i].first;while(printNode != NULL){cout << printNode->value << " -> ";printNode = printNode->next;}cout << "NULL" << endl;}}int main(){setgrapth();printGraph();cout << "hello world" << endl;return 0;}
代码所做功能
1、构建邻接表
2、打印邻接表
操作示例图:
- 构建图的邻接表
- 邻接链表的构建
- 邻接链表构建图
- 【数据结构】图的构建(邻接表法)
- 邻接表的构建、DFS、BFS搜索
- 邻接表构建模板
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 图的邻接表
- 无向图的构建及深度优先遍历---邻接表实现
- 无向图的构建及广度优先遍历---邻接表实现
- 邻接链表的构建详解【头插法&&尾插法】
- 图的邻接表操作
- http协议详细介绍
- cookie 和session 的区别详解
- 字符串函数大全
- poj 2392
- C++中^运算
- 构建图的邻接表
- C++中^的用法
- 16进制颜色代码(完全)
- 二叉树的非递归遍历(递归和非递归)
- 题目1368:二叉树中和为某一值的路径
- 几种session存储方式比较
- 谷歌C++测试
- VS未能正确加载 Microsoft.VisualStudio.Editor.......此处省略N个词
- 路由器设置的地址改了之后(路由器IP地址改了),忘记了,怎么找到这个IP?