胡图图的糊涂图

#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include "cstdlib"//syste()函数需要该头文件;

using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;

#define MaxInt 0 //网中表示极大值,即∞,若为无向图,Mxint 0;

#define MVNum 100 //最大顶点数
#define MENum 20  //最大边数


//图的邻接表存储表示
typedef char VerTexType; //假设顶点的数据类型为字符型
typedef int ArcType; //假设边的权值类型为整型,若为无向图,ArcType->EdgeType 
typedef struct ArcNode//边结点 
{
	int adjvex;  //改边所指向的顶点的位置 
	struct ArcNode *nextarc;//指向下一条边的指针
	//OtherInfo info; //和边相关的信息 
}ArcNode;
typedef struct VNode//顶点信息
{
	VerTexType data;
	ArcNode *firstarc;//指向第一条依附该顶点的边的指针 
 } VNode,AdjList[MVNum];//AdjList表示邻接表类型
 typedef  struct
 {
 	AdjList vertices;
 	int vexnum,arcnum;//图的当前顶点数和边数 
  } ALGraph;
  
  int LocateVex(ALGraph G,char ch)
{
	int i=0;
	for(i=0; i<G.vexnum;i++)
	{
		if(G.vertices[i].data==ch)
			break;
	}

	if(i>=G.vexnum) //如果超过顶点数量了,返回-1
		return -1;

	return i; //return the index of the vertex you are looking
} 
Status CreateUDG(ALGraph &G)
{//采用邻接表表示法,创建无向图G
    VerTexType v1,v2;
    cout<<"输入总顶点数,总边数:" ; 
    cin>>G.vexnum>>G.arcnum;//输入总顶点数,总边数
    cout<<"输入各点,构造表头结点表" ;
	for(int i=0;i<G.vexnum;i++)
	{
		cin>>G.vertices[i].data; //输入各点,构造表头结点表 
		G.vertices[i].firstarc=NULL;//初始化表头结点的指针域为NULL 
	 } 
	 cout<<"输入一条边依附的两个顶点:" ;
	 for(int k=0;k<G.arcnum;++k)
	 {
	 	cin>>v1>>v2;//输入一条边依附的两个顶点
		 int i=LocateVex(G,v1);
		 int j=LocateVex(G,v2);//确定v1和v2在G中的位置,即顶点在G.vertics中的序号
		 ArcNode *p1=new ArcNode;//生成一个新的边结点*p1 
		 p1->adjvex=j;//邻接点序号为j
		 p1->nextarc=G.vertices[i].firstarc;
		 G.vertices[i].firstarc=p1;
		 //将新的结点*p1插入顶点vi的边表头部
		 ArcNode *p2=new ArcNode;//生成另一个对称的新的边结点*p2 
		  p1->adjvex=i;//邻接点序号为i
		 p1->nextarc=G.vertices[j].firstarc;
		 G.vertices[j].firstarc=p2;
		 //将新的结点*p2插入顶点vj的边表头部
	  } 
	return OK;
}
bool visited[MVNum];//访问标志数组,初值为 false、
//采用邻接表表示图的深度优先搜索遍历图G 
void DFS_AL(ALGraph G,VerTexType v)
{
	cout<<v;visited[v]=true;//访问第v个顶点,并置访问标志数组相应分量值为true
	ArcNode *p=G.vertices[v].firstarc;//p指向v的边链表的第一个边结点
	while(p!=NULL)
	{
	    VerTexType w=p->adjvex;//表示w是v的邻接点
		if(!visited[w])  
		DFS_AL(G,w);//如果w未访问则递归调用DFS
		p=p->nextarc;//p指向下一个边结点 
		cout<<p->adjvex<<endl; 
	 } 
 } 
 
Status main()
{
	VerTexType v='a';
	ALGraph G;
	CreateUDG(G);
	//cout<<"输入深度优先遍历的出发点v:";
	//scanf("%c",&v);
	DFS_AL(G,v); 
//	PrintAMGraphArc(G);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值