//深度遍历是可以了,可是广度遍历却不行,实在找不到哪里错,没有语法错误码,似乎是哪里指针错了,调节器试后出现这种
//东西
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);
}