数据结构创建有向图(C++语言)

41 阅读1分钟

不要自卑,去提升实力
互联网行业谁技术牛谁是爹
如果文章可以带给你能量,那是最好的事!请相信自己
加油o~

代码:

/**
 *作者:魏宝航
 *2020年11月30日,下午21:22
 */
#include<iostream>
using namespace std;
class Graphic {
public:
	char vexs[999];
	int graphic[999][999];
	int num;
	Graphic(char vexs[], int edges[][3],int n) {
		num = n;
		for (int i = 0; i < n; i++) {
			this->vexs[i] = vexs[i];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (edges[i][j] == 0&&i!=j) {
					graphic[i][j] = INT_MAX;
				}
				else {
					graphic[i][j] = edges[i][j];
				}
			}
		}
	}
	void print() {
		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				if (graphic[i][j] == INT_MAX) {
					cout << '∞' << " ";
				}
				else {
					cout << graphic[i][j] << " ";
				}
			}
			cout << endl;
		}
	}
};
int main() {
	int arr[3][3] = { {0,2,3},{4,0,6},{7,8,0} };
	char vex[3] = { 'A','B','C' };
	Graphic* g = new Graphic(vex,arr,3);
	g->print();

}