【图】邻接矩阵和邻接表

一、邻接矩阵

#include<iostream>
using namespace std;

class AdjacencyMatrix{
public:
	typedef int VertexType;
	typedef int EdgeType;
	AdjacencyMatrix();
	~AdjacencyMatrix();
	void create_graph();
	void print_graph();
private:
	EdgeType** arc;
	int vertex_num;
	int edge_num;
};
AdjacencyMatrix::AdjacencyMatrix():arc(NULL),vertex_num(0),edge_num(0){}
AdjacencyMatrix::~AdjacencyMatrix(){
	if (arc != NULL){
		for (int i = 0; i < vertex_num; i++){
			if (arc[i] != NULL)
				delete[] arc[i];
		}
		delete[] arc;
	}
}
void AdjacencyMatrix::create_graph(){
	int i, j,k,w;
	cout << "请输入图的顶点数和边数:" << endl;
	cin >> vertex_num >> edge_num;
	arc = new EdgeType*[vertex_num];
	for (i = 0; i < vertex_num; i++)
		arc[i] = new EdgeType[vertex_num];
	for (i = 0; i < vertex_num; i++){
		for (j = 0; j < vertex_num; j++){
			arc[i][j] = 0;
		}
	}
	for (i = 0; i < edge_num; i++){
		cout << "输入边(vj,vk)上的上标j,下标k和权w:" << endl;
		cin >> j >> k >> w;
		arc[j][k] = arc[k][j] = w;
	}
}
void AdjacencyMatrix::print_graph(){
	int i, j;
	cout << "图的顶点:";
	for (i = 0; i < vertex_num; i++)
		cout<<i<<" ";
	cout << endl;
	cout << "图的邻接矩阵:" << endl;
	for (i = 0; i < vertex_num; i++){
		for (j = 0; j < vertex_num; j++)
			cout << arc[i][j] << '\t';
		cout << endl;
	}
	for (i = 0; i < vertex_num; i++){
		for (j = 0; j < vertex_num; j++){
			if (arc[i][j] != 0)
				cout << "输入边(vj,vk)上的上标" << i << ",下标" << j << "和权" << arc[i][j] << endl;
		}
	}
}
int main(){
	AdjacencyMatrix matrix;
	matrix.create_graph();
	matrix.print_graph();

	return 0;
}

二、邻接表

#include<iostream>
using namespace std;

typedef int VertexType;//顶点类型
typedef int EdgeType;//边的权值类型

struct edge_node{//边表结点 
	int adjvex;//邻接点域 
	EdgeType weight;// 权值 
	edge_node* next;//指向下一个邻接点 
	edge_node() :adjvex(0), weight(0), next(NULL){}
	edge_node(int a, EdgeType w) :adjvex(a), weight(w), next(NULL){}
};

struct vertex_node{//顶点表结点 
	VertexType data;//顶点域 
	edge_node* firstedge;//边表头指针 
	vertex_node() :data(0), firstedge(NULL){}
	vertex_node(VertexType d) :data(d), firstedge(NULL){}
};

class GraphAdjacencyList{
public:
	GraphAdjacencyList();
	~GraphAdjacencyList();
	void create_graph();
	void print_graph();
private:
	vertex_node* vertex;
	int vertex_num;
	int edge_num;
};
GraphAdjacencyList::GraphAdjacencyList():vertex(NULL),vertex_num(0),edge_num(0){}
GraphAdjacencyList::~GraphAdjacencyList(){
	if (vertex != NULL){
		for (int i = 0; i < vertex_num; i++){
			edge_node* head = vertex[i].firstedge;
			edge_node* cur = vertex[i].firstedge;
			while (cur != NULL){
				head = head->next;
				delete cur;
				cur = head;
			}
		}
		delete[] vertex;
	}
}
void GraphAdjacencyList::create_graph(){
	int i, j, k, w;
	edge_node* e;
	cout << "请输入图的顶点数和边数:" << endl;
	cin >> vertex_num >> edge_num;
	vertex = new vertex_node[vertex_num];
	for (i = 0; i < vertex_num; i++){
		cout << "请输入顶点:";
		cin >> vertex[i].data;
	}
	for (i = 0; i < edge_num; i++){
		cout << "输入边(vj,vk)上的顶点序号和权重:" << endl;
		cin >> j >> k >> w;

		e = new edge_node(k, w);
		e->next = vertex[j].firstedge;
		vertex[j].firstedge = e;

		e = new edge_node(j, w);
		e->next = vertex[k].firstedge;
		vertex[k].firstedge = e;
	}
}
void GraphAdjacencyList::print_graph(){
	for (int i = 0; i < vertex_num; i++){
		cout<<vertex[i].data<<" ";
		edge_node* cur = vertex[i].firstedge;
		while (cur != NULL){
			cout << cur->adjvex << " ";
			cur = cur->next;
		}
		cout << endl;
	}
}

int main(){
	GraphAdjacencyList g;
	g.create_graph();
	g.print_graph();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值