| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 756 人关注过本帖
标题:[求助]无向图的深度与广度遍历..
只看楼主 加入收藏
angelwlr
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-11-14
收藏
 问题点数:0 回复次数:3 
[求助]无向图的深度与广度遍历..

//深度遍历是可以了,可是广度遍历却不行,实在找不到哪里错,没有语法错误码,似乎是哪里指针错了,调节器试后出现这种
//东西
Q ERROR:cannot display value
v cxx0030:error:expression cannot evaluated
p cxx0030:error:expression cannot evaluated
//Loaded 'ntdll.dll', no matching symbolic information found.
//Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
//First-chance exception in 53l.exe: 0xC0000005: Access Violation.
//大家帮帮忙吧,,我改了好久了,

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define OVERFLOW -2
#define ERROR -1
#define NULL 0
#define OK 1
#define MAX_VEX_NUM 20
#define MAXQSIZE 20

typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *firstarc;
}VNode ;
typedef struct{
VNode Adjlist[1+MAX_VEX_NUM];
int vexnum,arcnum;
}ALGraph;
typedef struct Qnode
{
int data;
struct Qnode *next;
}Qnode,*QueuePtr;
typedef struct linkqueue
{
QueuePtr front;
QueuePtr rear;
}linkqueue;

linkqueue Q;
ALGraph G;
ArcNode *pg[1+MAX_VEX_NUM];//建邻接表所用的每个顶点的指针
int visite[1+MAX_VEX_NUM];//顶点是否被访问过的的标志

void creat();
void DFSTraverse();
void DFS(ALGraph G,int v);
void cread(ALGraph &G);
int FirstAdjVex(ALGraph G,int v);
int NextAdjVex(ALGraph G,int v,int u);
void BFSTraverse();
int EnQueue(linkqueue &Q,int e);


int InitQueue(linkqueue Q)
{
Q.front =Q.rear=(QueuePtr)malloc(sizeof(Qnode));
if(!Q.front )exit(OVERFLOW);
Q.front->next = NULL;
return OK;

}

int EnQueue(linkqueue &Q,int v)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(Qnode));
if(!p)exit(OVERFLOW);
p->data = v;
p->next = NULL;
Q.rear->next = p;
Q.rear =p;
return OK;

}

int DeQueue(linkqueue &Q)
{
Qnode *p;
int e;
if(Q.front==Q.rear)return ERROR;
p= Q.front ->next ;
e=p->data ;
Q.front->next = p->next ;
if(Q.rear ==p)Q.rear = Q.front ;
free(p);
return e ;
}
int QueueEmpty(linkqueue Q)
{
if(Q.front==Q.rear)return OK;
else return ERROR;
}

void creat(ALGraph &G)
{

int i;
int m,n;
ArcNode *q1,*q2;
for(i=1;i<=G.vexnum;i++)
{
G.Adjlist[i].data=i;//初始化顶点名。用数字从1开始。
G.Adjlist[i].firstarc=NULL;
pg[i]= G.Adjlist[i].firstarc;

}
for(i=0;i<G.arcnum;i++)
{
printf("the vex1:"); scanf("%d",&m);
printf("the vex2:"); scanf("%d",&n);
q1=(ArcNode *)malloc(sizeof(ArcNode));
q1->adjvex=n;q1->nextarc=NULL;
q2=(ArcNode *)malloc(sizeof(ArcNode));
q2->adjvex=m;q2->nextarc=NULL;
if(!G.Adjlist[m].firstarc)//第一弧为空。
{
G.Adjlist[m].firstarc=q1;
pg[m]=q1->nextarc;
}
else //第一弧不空。
{
pg[m]=q1;
pg[m]=q1->nextarc;
}

if(!G.Adjlist[n].firstarc)
{
G.Adjlist[n].firstarc=q2;
pg[n]=q2->nextarc;
}
else
{
pg[n]=q2;
pg[n]=q2->nextarc;
}
}
}

void DFSTraverse(ALGraph G)
{
int v;
for(v=1;v<=G.vexnum;++v)visite[v]=0;
for(v=1;v<=G.vexnum;++v)
{
if(!visite[v])DFS(G,v);
}
}
void DFS(ALGraph G,int v)
{
int w;
visite[v]=1;printf("%d,",v);
for(w=FirstAdjVex(G,v);w>0;w=NextAdjVex(G,v,w))
if(!visite[w])DFS(G,w);
}
int FirstAdjVex(ALGraph G,int v)//在图G中寻找第v个顶点的第一个邻接顶点
{
if(!G.Adjlist[v].firstarc) return 0;
else return(G.Adjlist[v].firstarc->adjvex);
}

int NextAdjVex(ALGraph G,int v,int u)//在图G中寻找第v个顶点的相对于u的下一个邻接顶点
{
ArcNode *p;
p=G.Adjlist[v].firstarc;
while(p->adjvex!=u) p=p->nextarc; //在顶点v的弧链中找到顶点u
if(p->nextarc==NULL) return 0; //若已是最后一个顶点,返回0
else return(p->nextarc->adjvex); //返回下一个邻接顶点的序号
}
void BFSTraverse(ALGraph G)
{
int v,w,u;
for(v=1;v<=G.vexnum;++v)visite[v]=0;
InitQueue(Q);
for(v=1;v<=G.vexnum;++v)
if(!visite[v])
{
visite[v]=1;
printf("%d,",v);
EnQueue(Q,v);
while(!QueueEmpty(Q))
{
u=DeQueue(Q);
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
if(!visite[w])
{
visite[w]=1;printf("%d,",w);
EnQueue(Q,w);

}//if
}//while
}//if
}

void main()
{
printf("input the vexnum of the graph:");
scanf("%d",&G.vexnum);
printf("input the arcnum of the graph:");
scanf("%d",&G.arcnum);
creat(G);

printf("deepfisrt:");
DFSTraverse(G);
printf("\nbroadfist:");
BFSTraverse(G);

}

搜索更多相关主题的帖子: 遍历 深度 
2006-12-25 23:43
superkuuga
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-8-1
收藏
得分:0 
好长啊,明天找个时间帮你研究下

2006-12-26 00:51
xingyu23
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-12-21
收藏
得分:0 
楼上的.在吗?帮个忙!~
2006-12-26 00:54
angelwlr
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-11-14
收藏
得分:0 

一楼的,先谢谢了,,指针搞得头好晕.

2006-12-26 23:33
快速回复:[求助]无向图的深度与广度遍历..
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012047 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved