有向图的深度优先遍历

Linus27 2010-11-05 09:18:38
编译通过,但调试时报错Access Violation,报错位置在DFS函数里,求达人指教。


typedef int Status;
typedef int VRType;
typedef char VertexType;
typedef char InfoType;

char temp;
int visited[MAX_VNUM];

typedef struct ArcNode{ //表结点
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //该弧相关信息的指针
}ArcNode;

typedef struct VNode{
VertexType data;//顶点信息
ArcNode *firstarc;//指向第一个表结点
}VNode, AdjList[MAX_VNUM];

typedef struct{
AdjList vertices;
int vexnum,arcnum; //顶点数和边数
int kind; //图的种类标志
}ALGraph;

int LocateVex(ALGraph G,VertexType u)
{ // 初始条件: 图G存在,u和G中顶点有相同特征
// 操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1
int i;
for(i=0;i<G.vexnum;++i)
if(u==G.vertices[i].data)
return i;
return -1;
}

int FirstAdjVex(ALGraph G,VertexType v)
{ // 初始条件: 图G存在,v是G中某个顶点
// 操作结果: 返回v的第一个邻接顶点的序号。
//若顶点在G中没有邻接顶点,则返回-1
ArcNode *p;
int v1;
v1=LocateVex(G,v); // v1为顶点v在图G中的序号
p=G.vertices[v1].firstarc;
if(p)
return p->adjvex;
else
return -1;
}

int NextAdjVex(ALGraph G,VertexType v,VertexType w)
{ // 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点
// 操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。
// 若w是v的最后一个邻接点,则返回-1
ArcNode *p;
int v1,w1;
v1=LocateVex(G,v); // v1为顶点v在图G中的序号
w1=LocateVex(G,w); // w1为顶点w在图G中的序号
p=G.vertices[v1].firstarc;
while(p&&p->adjvex!=w1) p=p->nextarc;
if(!p||!p->nextarc) //没找到w或w是最后一个邻接点
return -1;
else //p->adjvex==w
return p->nextarc->adjvex;
}
int CreateDG(ALGraph &G) /* 邻接表建立有向图 */
{
int i,s,d;
ArcNode *p;
printf("请输入顶点数和弧数");
scanf("%d%d",&G.vexnum,&G.arcnum); /* 输入结点数目和边数 */
getchar();
for(i=1;i<=G.vexnum;i++) /* 输入顶点 */
{
printf("请输入第%d个顶点",i);
scanf("%c",&G.vertices[i].data); /* 输入顶点 */
getchar();
G.vertices[i].firstarc=NULL; /* 首先初始化为NULL */
}
for(i=1;i<=G.arcnum;i++)
{
printf("请输入第%d条弧头和弧尾",i);
scanf("%d%d",&s,&d); /* 输入一条边依附的起点序号和终点序号 */
getchar();
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d; /* 保存该弧所指向的终点位置 */
/* 这两句代码相当于单链表的插入操作 */
p->nextarc=G.vertices[s].firstarc; /* 保存顶点所对应的终点位置 */
G.vertices[s].firstarc=p;
}
return OK;
}
void DFS(ALGraph G,int v)
{
visited[v]=TRUE;
printf("%2c",G.vertices[v].data);
ArcNode *q;
for(q=G.vertices[v].firstarc;q;q=q->nextarc)
if (!visited[q->adjvex])
DFS(G,q->adjvex);
}




void DFSTraverse(ALGraph & G)
{
int v;
for(v=0;v<G.vexnum;++v) visited[v]=FALSE;
for(v=0;v<G.vexnum;++v)
if(!visited[v])
DFS(G,v);
}

int main()
{
ALGraph G;
CreateDG(G);
DFSTraverse(G);

system("pause");
return 0;
}
...全文
1916 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
無_1024 2010-11-05
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
#define M 100
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define QUEUE_SIZE 101
typedef struct ArcNode
{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
}ArcNode;
typedef char VerterType; //表示弧的节点
typedef struct VNode
{
VerterType data; //顶点信息
ArcNode *firstarc; //指向第一条衣服该顶点的弧的指针
}VNOde,AdjList[M];
typedef struct
{
AdjList vertices; //图的顶点信息
int vexnum,arcnum; //图的当前顶点数和弧数
}ALGraph;

typedef struct{
int *base;
int front;
int rear;
}queue; //队列结构体
void InitQueue(queue &q){ //初始化队列
q.base=(int *)malloc(QUEUE_SIZE*sizeof(int));
q.front=q.rear=0;
}
int EnQueue(queue &q,int e){ //入队
q.base[q.rear]=e;
q.rear=(q.rear+1)%QUEUE_SIZE;
return 1;
}
int DeQueue(queue &q,int &e){//出队
e=q.base[q.front];
q.front=(q.front+1)%QUEUE_SIZE;
return 1;
}
int visited[M];//便于记录是否已经访问

int LocateVex(ALGraph G,VerterType u) //找到一顶点u有弧相接的顶点并返回顶点编号
{
int i;
for(i=0;i<G.vexnum;++i)
{
if(u == G.vertices[i].data)
return i;
}
return -1;
}

int Createg(ALGraph &G) //构建图的邻接表的存储方式
{
int v,a,m,i,j,weight[M];
ArcNode *p,*q;
VerterType t,h;
printf("请输入顶点数: ");
scanf("%d",&v);
if(v<0)
return ERROR;
G.vexnum=v;
fflush(stdin);
printf("请输入弧数: ");
scanf("%d",&a);
if(a<0)
return ERROR;
G.arcnum=a;
fflush(stdin);
printf("请输入%d个顶点\n",v);
for(m=0;m<v;m++)
{
printf("请输入顶点%d: ",m);
G.vertices[m].data=getchar();
G.vertices[m].firstarc=NULL;
fflush(stdin);
}
printf("请输入%d条弧\n",a);
fflush(stdin);
for(m=0;m<a;m++)
{
printf("请输入弧%d: ",m);
scanf("%c %c %d",&t,&h,&weight[m]);
i=LocateVex(G ,t);
j=LocateVex(G ,h);
if(i<0)
return ERROR;
if(j<0)
return ERROR;
p=(ArcNode *)malloc(sizeof(ArcNode));
if(!G.vertices[i].firstarc)
G.vertices[i].firstarc=p;
else
{
q=G.vertices[i].firstarc;
while(q->nextarc)
q=q->nextarc;
q->nextarc=p;
}
p->adjvex=j;
p->nextarc=NULL;
fflush(stdin);
}
return OK;
}
VerterType ReturnValue(ALGraph G,int i) //返回编号为i的图节点信息
{
if(i>=0&&i<G.vexnum)
return G.vertices[i].data;
else
{
printf("ERROR!\n");
exit(i);
}
}
int ReturnFirstNeighbor(ALGraph G,int v) //返回与顶点v有弧相接的顶点编号
{
ArcNode *p;
if(v!=-1)
{
p=G.vertices[v].firstarc;
if(p!=NULL)
return p->adjvex;
return -1;
}
return -1;
}
int ReturnNextNeighbor(ALGraph G,int vi,int vj)
{
ArcNode *p;
if(vi!=-1)
{
p=G.vertices[vi].firstarc;
while(p!=NULL)
{
if(p->adjvex==vj&&p->nextarc!=NULL)
return p->nextarc->adjvex;
else
p=p->nextarc;
}
}
return -1;
}
void dfs(ALGraph G,int v)
{
int u;
printf("%c ",ReturnValue(G,v));
visited[v]=TRUE;
u=ReturnFirstNeighbor(G,v);
while(u!=-1)
{
if(!visited[u])
dfs(G,u);
u=ReturnNextNeighbor(G,v,u);
}
}
void DFS(ALGraph G) //深度遍历
{
int i;
for(i=0;i<G.vexnum;i++)
visited[i]=FALSE;
for(i=0;i<G.vexnum;i++)
{
if(!visited[i])
dfs(G,i);
}
}
void BFS(ALGraph G){ //广度遍历
int k;
for(int v=0;v<G.vexnum;v++)
visited[v]=FALSE;
queue q;
InitQueue(q);
for(int i=0;i<G.vexnum;i++)
if(!visited[i]){
visited[i]=TRUE;
printf("%c ",G.vertices[i].data);
EnQueue(q,i);
while(q.front!=q.rear){
DeQueue(q,k);
for(int w=ReturnFirstNeighbor(G,k);w>=0;w=ReturnNextNeighbor(G,k,w))
if(!visited[w]){
visited[w]=TRUE;
printf("%c ",G.vertices[w].data);
EnQueue(q,w);
}
}
}
}

void printfAdjList(ALGraph G)
{
int i;
ArcNode *p;
printf("%s,%s,%s\n","编号","顶点","相邻边编号");
for (i=0;i<G.vexnum;++i)
{
printf(" %d %c ",i,G.vertices[i].data);
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
printf(" %d %c ", p->adjvex,G.vertices[p->adjvex].data);
printf("\n");
}
}

int main()
{
ALGraph G;
Createg(G);
printf("\n邻接表为:");
printfAdjList(G);
printf("深度遍历:");
DFS(G);
printf("\n广度遍历:");
BFS(G);
printf("\n");
system("pause");
return 0;
}


LZ参考一下吧 这是我以前自己写的 参考参考吧
luciferisnotsatan 2010-11-05
  • 打赏
  • 举报
回复
访问违规,应该是指针问题。
lz调试时注意下指针指向的位置

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧