为什么遍历老是错误

#include<iostream>
#include<queue>
using namespace std;
#define MVNum 100
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define MaxInt 100
#define MAXQSIZE 100
typedef int status;
typedef int VerTexType;
typedef int ArcType;
typedef int QElemType;
typedef int OtherInfor;
typedef struct
{
 QElemType *base;
 int front;
 int rear;
}SqQueue;
typedef struct
{
 VerTexType vexs[MVNum];
 ArcType arcs[MVNum][MVNum];
 int vexnum,arcnum;
}AMGraph;
typedef struct ArcNode
{
 int adjvex;
 struct ArcNode *NextAdjVex;
 OtherInfor info;
}ArcNode;
typedef struct VNode
{
 VerTexType date;
 ArcNode *FirstAdjVex;
 
}VNode,AdjList[MVNum];
int LocateVex(AMGraph G,VerTexType v)
{
 int i;
 for(i=0;i<G.vexnum;++i)
  if(v==G.vexs[i])
   return i;
  else
   return ERROR;
}
status CreatUDN(AMGraph &G);
void DFS(AMGraph G,int v);
void BFS(AMGraph G,int v);
void DFSTraverse(AMGraph G);
status InitQueue(SqQueue &Q);
status EnQueue(SqQueue &Q, int e);
status DeQueue(SqQueue &Q,int &e);
status CreatUDN(AMGraph &G)
{
 int i,j,k,w,v1,v2;
 cout << "输入顶点数、边数" << endl;
 cin>>G.vexnum>>G.arcnum;
 for(i=0;i<G.vexnum;i++)
 {
  cout << "输入点的信息" << endl;
  cin>>G.vexs[i];
 }
 for(i=0;i<G.vexnum;++i)        
 {
  for(j=0;j<G.vexnum;++j)
  {
   G.arcs[i][j]=0;
  }   
 } 
 for(k=0;k<G.arcnum;++k)
 {
  cout << "输入一条边依附的顶点 " << endl;
  cin>>v1>>v2;
  //i=LocateVex(G,v1);
  //j=LocateVex(G,v2);
  G.arcs[v1-1][v2-1]=1;
  G.arcs[v2-1][v1-1]=G.arcs[v1-1][v2-1];  
 }
 return OK;
}
int visited[MVNum];
void DFS(AMGraph G,int v)
{
 int w;
 cout<<v;visited[v]=true;
 for(w=0;w<G.vexnum;w++)
  if((G.arcs[v][w]!=0)&&(!visited[w]))
   DFS(G,w);
}
status InitQueue(SqQueue &Q)
{
 Q.base=new QElemType[MAXQSIZE];
 if(!Q.base) exit(OVERFLOW);
 Q.front=Q.rear=0;
 return OK;
}
status EnQueue(SqQueue &Q, int e) 
{
    if((Q.rear+1)%MAXQSIZE==Q.front) 
        return ERROR; 
    Q.base[Q.rear]=e; 
    Q.rear=(Q.rear+1)%MAXQSIZE;
    return OK;
}
status DeQueue(SqQueue &Q,int &e) 
{
 if(Q.front==Q.rear)
 return ERROR;
    e=Q.base[Q.front]; 
    Q.front=(Q.front+1)%MAXQSIZE;
 return OK; 

status QueueEmpty(SqQueue Q) 
{    
    if(Q.rear==Q.front) 
        return OK;
 else
        return ERROR; 

int FirstAdjVex(AMGraph G,int v) 
{/*v的第一个邻接点*/ 
    int i; 
    for(i=0;i<G.vexnum;++i) 
    { 
        if(G.arcs[v][i]==1&&visited[i]==false) 
            return i; 
    } 
    return ERROR; 

int NextAdjVex(AMGraph G,int v,int w) 
{/*v相对于w的下一个邻接点*/ 
    int i; 
    for(i=w;i<G.vexnum;++i) 
    { 
        if(G.arcs[v][i]==1&&visited[i]==false) 
            return i; 
    } 
    return ERROR; 
}  
void BFS(AMGraph G,int v)

 int u,w;
 SqQueue Q;
 cout<<v;visited[v]=true;
 InitQueue(Q); 
 EnQueue(Q,v);
 while(!QueueEmpty(Q))
 {
  int w;
  DeQueue(Q,u);
  for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
  if(!visited[w])
  {
   cout<<w;visited[w]=true;
   EnQueue(Q,w);
  }
 }
}
int main()
{
 int i,v;
 AMGraph G;
 CreatUDN(G);
 cout<<"深度优先遍历:"<< endl;
 DFS(G,v);
 cout<<"广度优先遍历:"<< endl;
 BFS(G,v);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值