0
回答
如何在这个代码里面加入边的权值 求大神们指教
#include <iostream>
using namespace std;
typedef struct edge
{
	char AdjVertex;
	int Weight;
	struct edge *NextEdge;
}EdgeType;
typedef struct 
{
	char Vertex;
	EdgeType *FirstEdge;
}VertexType;
typedef VertexType adjlist[100];
adjlist Graph;
static int n;
static int e;
//static int w;
int LocVertex(adjlist q, char x)
{
	for (int k = 0; k < n; k++)
	{
		if (x==q[k].Vertex)
		{
			return k;
		}
		else
		{
			continue;
		}
	}
	return 0;
}
int CreateAdjlist()
{
	int i,j;
	char v1, v2;//边结点
	EdgeType *p, *q;
	cout << "输入图中顶点的个数n和边数e" << endl;
	cin >> n;
	cin >> e;
	cout << "输入顶点的数据:";
	for (int k = 0; k < n;k++)
	{
		cin >> Graph[k].Vertex;
		Graph[k].FirstEdge = NULL;
	}
	cout << "输入图中各边" << endl;
	for (int k = 0; k < e; k++)
	{
		cin >> v1 >> v2;
		i = LocVertex(Graph, v1);
		j = LocVertex(Graph, v2);
		q = new EdgeType;
		q->AdjVertex = j;
		q->NextEdge = Graph[i].FirstEdge;
		Graph[i].FirstEdge = q;
		

		p = new EdgeType;
		p->AdjVertex = i;
		p->NextEdge = Graph[j].FirstEdge;
		Graph[j].FirstEdge = p;
		
		
	}
	cout << "图的邻接表结构;"<<endl;
	for (i = 0; i < n; i++)
	{
		cout << "i=" << i << endl;
		v1 = Graph[i].Vertex;
		cout << "Vertex:" << v1;
		p = Graph[i].FirstEdge;
		while (p!=NULL)
		{
			v2 = Graph[p->AdjVertex].Vertex;
			cout << "-->" << v2 <<" ";
			p = p->NextEdge;
		}
		cout << endl;
	}
	return 0;
}
void main()
{
	CreateAdjlist();
}



如果想要在边上加入权值 应该怎么加


举报
lzjuypf
发帖于1年前 0回/310阅
顶部