} 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
运行结果:
|