数据结构C语言-图的建立以及深度优先、广度优先遍历

本文详细介绍了如何使用C语言构建图的数据结构,并实现了深度优先搜索和广度优先搜索的遍历算法。通过具体的代码展示了这两种遍历方法在图操作中的应用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef int bool;
#define true 1
#define false 0

//邻接矩阵
#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;
typedef struct
{
   
    VerTexType vexs[MVNum];
    ArcType arcs[MVNum][MVNum];
    int vexnum,arcnum;
}AMGraph;

int LocateVex_AM(AMGraph G,VerTexType v)
{
   
    int i=0;
    //while(i<G.vexnum&&v!=G.vexs[i])
        //i++;
    for(;i<G.vexnum;i++)
        if(G.vexs[i]==v)
           break;
    return i;
}

status CreateUDN_AM(AMGraph *G,VerTexType *V)
{
   
    int i,j,k;
    for(i=0;i<G->vexnum;i++)
        G->vexs[i]=V[i];
    for(i=0;i<G->vexnum;i++)
        for(j=0;j<G->vexnum;j++)
        G->arcs[i][j]=MaxInt;
    char v1,v2;
    for(k=0;k<G->arcnum;k++)
    {
   
        scanf("%c%c",&v1,&v2);
        getchar();//吃回车!!
        int i=LocateVex_AM(*G,v1);
        int j=LocateVex_AM(*G,v2);
        G->arcs[i][j]=1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值