图的遍历 邻接表

/*有问题*/
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
typedef char  VerTexType ;
typedef int OtherInfo  ;
const int MVnum=100;   //MaxVertexNumber
typedef struct ArcNode
{
	int adjvex;   //邻接点的序号
	ArcNode *nextarc;
	OtherInfo info;   
}ArcNode;
typedef struct VNode
{
	VerTexType date;
	ArcNode *firstarc;
}AdjList[MVnum];

typedef struct 
{
	AdjList vertices;
	int vexnum,arcnum;
}ALG;
int Locate(ALG G,VerTexType v)
{
	for(int i=1;i<=G.vexnum;i++)
		if(G.vertices[i].date==v)
			return i;
	return -1;
}
void Creat_UDN(ALG &G)
{
	cin>>G.vexnum>>G.arcnum;
	for(int i=1;i<=G.vexnum;i++)
	{
		cin>>G.vertices[i].date;
		G.vertices [i].firstarc=NULL;
	}
	for(int i=1;i<=G.arcnum;i++)
	{
		VerTexType v1,v2;
		OtherInfo w;
		cin>>v1>>v2>>w;
		int ii,jj;
		ii=Locate(G,v1);
		jj=Locate(G,v2);
		ArcNode p1,p2;
		p1.adjvex=jj;
		p1.nextarc=G.vertices [ii].firstarc;
		G.vertices[ii].firstarc=&p1;
		p1.info=w;

		p2.adjvex=ii;
		p2.nextarc=G.vertices[jj].firstarc;
		p2.info =w;
		G.vertices[jj].firstarc=&p2;
	}
}
void Print(ALG G)
{
	for(int i=1;i<=G.vexnum ;i++)
	{
		cout<<G.vertices[i].date<<"  ";
		ArcNode p;
		p.nextarc=G.vertices[i].firstarc;
		while((p.adjvex  )!=NULL)
		{
			cout<<G.vertices[p.adjvex].date <<" ";
			p.adjvex =p.nextarc->adjvex  ;
		}
	}
}
int main()
{
	ALG G;
	Creat_UDN(G);
	Print(G);
	return  0;
}
/*
5 4
b w s j w
b t  8
b j 9
t s 10
t w 32
*/
posted @ 2022-06-01 12:14  DuJunlong  阅读(10)  评论(0编辑  收藏  举报  来源