Data Structure——图的遍历

#include<iostream>
using namespace std; 
#define MaxInt 40000
#define MVNum 100
#define true 1
#define OK 1
#define MAXQSIZE 100
#define OVERFLOW -1
#define ERROR 0  
typedef int Status;
typedef char VerTexType;
typedef int ArcType;
typedef struct{    
    int* base;    
    int front;    
    int rear;
}SqQueue;
typedef struct{    
    VerTexType vexs[MVNum];    
    ArcType arcs[MVNum][MVNum];    
    int vexnum, arcnum;
} AMGraph; 
Status InitQueue(SqQueue& Q){
//构造一个空队列Q    
    Q.base = new int[MAXQSIZE];    
    if (!Q.base)  
    exit(OVERFLOW);    
    Q.front = Q.rear = 0;    
    return OK;

Status EnQueue(SqQueue& Q, int e){
//插入元素e为Q的新的队列元素    
    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){
//删除Q的队头元素,用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.front == Q.rear)        
    return OK;    
    return ERROR;

Status LocateVex(AMGraph G, VerTexType v){    
    int i;    
    for (i = 0; i < MVNum; i++)    {        
    if (G.vexs[i] == v)            
    return i;    
    }

Status CreateUDN(AMGraph& G){
//采用邻接矩阵表示法,创建无向网G        
    puts("请输入点数和边数:");    
    cin >> G.vexnum >> G.arcnum;    
    puts("请输入顶点集:");    
    for (int i = 0; i < G.vexnum; ++i)        
    cin >> G.vexs[i];    
    for (int i = 0; i < G.vexnum; ++i)        
    for (int j = 0; j < G.vexnum; ++j)
    G.arcs[i][j] = MaxInt;    
    puts("请输入边的集合:");    
    VerTexType v1, v2;    
    int  i, j;    
    for (int k = 0; k < G.arcnum; ++k)    {
        cin >> v1 >> v2;        
        i = LocateVex(G, v1);        
        j = LocateVex(G, v2);        
        G.arcs[i][j] = 1;        
        G.arcs[j][i] = G.arcs[i][j];    
        }    
        return OK;
        } 
        int FirstAdjVex(AMGraph G, int v){    
        for (int i = 0; i < G.vexnum; i++)    {        
        if (G.arcs[v][i] != MaxInt)        {
            return i;        
            }    
        }    
        return -1;
    } 
    int NextAdjVex(AMGraph G, int v, int w){    
    for (int i = w + 1; i < G.vexnum; i++)    {        
    if (G.arcs[v][i] != MaxInt)        {        
        return i;        
        }    
    }    
    return -1;

bool visited[MVNum];
void DFS(AMGraph G, int v){
//图G为邻接矩阵类型,从第V个顶点出发深度优先搜索遍历图G    
    cout << G.vexs[v];      //访问第v个顶点,并置访问标志数组相应分量值为true 
    visited[v] = true;       
    for (int w = FirstAdjVex(G, v);  //依次检查邻接矩阵v所在的行 
    w >= 0; 
    w = NextAdjVex(G, v, w)){        
    if (!visited[w])            
    DFS(G, w);    
    }

void BFS(AMGraph G, int v){//按广度优先非递归遍历连通图G    
    int u, w;    
    bool visited[MVNum];    
    SqQueue Q;    
    cout << G.vexs[v];    
    visited[v] = true;    //访问第v个顶点,并置访问标志数组相应分量值为true 
    InitQueue(Q);    //辅助队列Q初始化、置空 
    EnQueue(Q, v);    //v进队 
    while (!QueueEmpty(Q))    {  //队列非空        
    DeQueue(Q, u);        //队头元素出队并置为u 
    for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w))            
    //依次检查u的所有邻接点w,FirstAdjVex(G,U)表示u的第一个邻接点           
    //NextAdjVex(G,u,w)表示u相对于w的下一个邻接点,w>=0表示存在邻接点            
    if (!visited[w]){                //w为u的尚未访问的邻接顶点 
        cout << G.vexs[w];                //访问w,并置访问标志数组相应分量值为true 
        visited[w] = true;                
        EnQueue(Q, w);            //w进队 
        }    
    }

int main(){    
    int v;    
    AMGraph G;    
    CreateUDN(G);    
    int choice;    
    puts("请输入选项:");    
    puts("0:DFS遍历");     
    puts("1:BFS遍历");     
    puts("2:结束程序");    
    puts("");     
    while(cin>>choice){
        if(choice == 0){            
        printf("DFS遍历序列为:");            
        DFS(G, 0);            
        puts("");            
        puts("");    
        }                        
        else if(choice == 1){            
        printf("BFS遍历序列为:");            
        BFS(G, 0);            
        puts("");            
        puts("");        
        }
        else{
        puts("程序结束");            
        break;    
        }                    
        puts("0:DFS遍历");         
        puts("1:BFS遍历");         
        puts("2:结束程序");     
        }        
    return OK;
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值