设为首页 加入收藏

TOP

数据结构与算法――有向无环图的拓扑排序C++实现(三)
2016-04-30 15:25:09 】 浏览:692
Tags:数据结构 算法 向无环 拓扑 排序 实现
} Graph G(topo, edge_num); G.print(); vector topoSortArr = G.topoSort(); cout << "拓扑排序的结果: "; for(unsigned i = 0; i < topoSortArr.size(); ++i) cout << topoSortArr[i] << " "; cout << endl; release_buff(topo, edge_num); return 0; } /**************************************************************** * 函数名称:read_file * 功能描述: 读取文件中的图的数据信息 * 参数列表: buff是将文件读取的图信息保存到buff指向的二维数组中 * spec是文件中图最大允许的边的个数 * filename是要打开的图文件 * 返回结果:无 *****************************************************************/ int read_file(char ** const buff, const unsigned int spec, const char * const filename) { FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Fail to open file %s, %s.\n", filename, strerror(errno)); return 0; } printf("Open file %s OK.\n", filename); char line[MAX_LINE_LEN + 2]; unsigned int cnt = 0; while ((cnt < spec) && !feof(fp)) { line[0] = 0; fgets(line, MAX_LINE_LEN + 2, fp); if (line[0] == 0) continue; buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2); strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1); buff[cnt][4001] = 0; cnt++; } fclose(fp); printf("There are %d lines in file %s.\n", cnt, filename); return cnt; } /**************************************************************** * 函数名称:release_buff * 功能描述: 释放刚才读取的文件中的图的数据信息 * 参数列表: buff是指向文件读取的图信息 * valid_item_num是指图中边的个数 * 返回结果:void *****************************************************************/ void release_buff(char ** const buff, const int valid_item_num) { for (int i = 0; i < valid_item_num; i++) free(buff[i]); }

测试用例:

0,1,2,1
1,1,3,1
2,1,4,1
3,2,4,1
4,2,5,1
5,3,6,1
6,4,3,1
7,4,6,1
8,4,7,1
9,5,4,1
10,5,7,1
11,7,6,1

运行结果:



首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[C++]右值引用和转移语义 下一篇LeetCode:Counting Bits

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目