算法导论22.1-2答案---图论

分别用邻接矩阵,邻接表实现图的转置~~~~

#include<iostream>
using namespace std;
/**1:邻接链表
	*动态申请二维数组空间
**/
class Edge {
	friend class Grapth_List;
	int VerAdj;//边终点
	int cost;//权值
	Edge*link;//指向下一个边结点
};
class Vertex{
	friend class Grapth_List;
	int vername;//点名字
	Edge*adjacent;//链表边指针
};
class Grapth_List {
public:
	Vertex*Head;
	int graphsize;//图中顶点的个数

	Grapth_List(int n,int m) { 
		graphsize = n; 
		Head = new Vertex[n];
		for (int i = 0; i < n; i++) { 
			Head[i].adjacent = NULL;
			Head[i].vername = i;
		}
		for (int i = 0; i < m; i++) {//初始化
			int v1, v2, len;
			cin >> v1 >> v2 >> len;
			Edge*temp = Head[v1].adjacent;
			Edge*head = Head[v1].adjacent;
			while (temp != nullptr) {
  				temp = temp->link;
			}
			temp = new Edge();
			temp->cost = len;
			temp->VerAdj = v2;
			temp->link = NULL;
			if (head == NULL) {
				Head[v1].adjacent = temp;
				continue;
			}
			while (head != NULL) {
				if (head->link == NULL) {
					head->link = temp;
					break;
				}
				head = head->link;
			}
		}
	};
	void out() {
		for (int i = 0; i < graphsize; i++) {
			cout << i << ": ";
			Edge*temp = Head[i].adjacent;
			while (temp!=NULL) {
				cout  <<temp->VerAdj << "--长度:" << temp->cost<<";  ";
				temp = temp->link;
			}
			cout << endl;
		}
	}
	Grapth_List* zhuanzhi() {
		Vertex*headTemp = new Vertex[graphsize];
		for (int i = 0; i < graphsize; i++) {
			headTemp[i].adjacent = NULL;
			headTemp[i].vername = i;
		}
		for (int i = 0; i < graphsize; i++) {

			Edge*temp = Head[i].adjacent;
			int v2 = i;
			while(temp!=NULL) {
				int v1 = temp->VerAdj;
				Edge* ttemp = headTemp[v1].adjacent;
				Edge* head = headTemp[v1].adjacent;
				while (ttemp!=NULL) {
					ttemp = ttemp->link;
				}
				ttemp = new Edge();
				ttemp->VerAdj = v2;
				ttemp->cost = temp->cost;
				ttemp->link = NULL;
				if (head == NULL) {
					headTemp[v1].adjacent = ttemp;
					temp = temp->link;
					headTemp[v1].adjacent->link = NULL;
					continue;
				}
				while (head != NULL) {
					if (head->link == NULL) {
						head->link = ttemp;
						temp = temp->link;
						head->link->link = NULL;
						break;
					}
					head = head->link;
				}
			}
		}
		Head = headTemp;
		return this;
	}
};
/**2:邻接矩阵
	*动态申请二维数组空间
**/
void usingjuzheng(int n,int m) {
	int** map= new int*[n];
	for (int i = 0; i < n; i++) {
		map[i] = new int[n];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			map[i][j] = -1;//表示不连通
		}
	}
	//存储边
	for (int i = 0; i < m; i++) {
		int v1, v2, len;
		cin >> v1 >> v2 >> len;
		map[v1][v2] = len;
	}
	//out before:
	cout <<endl<< "此时邻接矩阵为:" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cout << map[i][j] << "  ";
		}
		cout << endl;
	}
	//转换:
	int**maptemp = new int*[n];
	for (int i = 0; i < n; i++) maptemp[i] = new int[n];

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			maptemp[i][j] = map[j][i];
		}
	}
	//out after:
	cout <<endl<< "完成转置后:" << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cout << maptemp[i][j] << "  ";
		}
		cout << endl;
	}
}

void usinglianbiao(int n, int m) {
	Grapth_List map(n,m);
	cout << endl<<"构成的邻接矩阵为:" << endl;
	map.out();
	cout << endl<<"转置后的邻接矩阵为:" << endl;
	map.zhuanzhi()->out();
}
int main() {
	int n, m;//图中有n个点,m条边
	cin >> n >> m;
	//邻接矩阵实现转置
	string choose;
	cout << "选择实现转置的方式:邻接矩阵/邻接链表,请输入1/2" << endl;
	int select;
	cin >> select;
	if (select == 1)
		usingjuzheng(n, m);
	else if (select == 2)
		usinglianbiao(n, m);
	else
		cout << "出错啦" << endl;
	
	return 0;
}

阿西,指针这块用的乱死了啊啊啊啊啊啊我好菜,这样一道题写了大半天。。。。。。。哭死了哭死了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值