C++实现数据结构图的深度和广度遍历(邻接表)

4 篇文章 0 订阅
3 篇文章 0 订阅
#include<iostream>
using namespace std;
#define MAXNUM 100
typedef struct Anode {

    int num;
    struct Anode* next;


}Anode;
typedef struct Vnode {

    string data;
    Anode* first;

}Vnode, Adjlist[MAXNUM];
typedef struct {
    Adjlist ver;
    int vexnum, arcnum;//图当前的顶点数和边数

}ALGRAPH;
typedef struct
{
    int* datas;  //循环数组
    int rear;
    int head;
}Queue;          //队列
/****************初始化队列***********************/
void Initqueue(Queue& q)
{
    q.datas = new int[MAXNUM];
    q.head = q.rear = 0;
}

/****************队列的插入(入队列)***********************/
void Insertqueue(Queue& q, int e)
{
    if ((q.rear + 1) % MAXNUM == q.head)
    {
        cout << "队列已满!!!" << endl;//实际此时还有一个空位
        return;
    }
    else
    {
        q.datas[q.rear] = e;
        q.rear = (q.rear + 1) % MAXNUM;
    }
}

/****************队列的删除(出队列)***********************/
int Deletequeue(Queue& q)
{
    int e;
    e = q.datas[q.head];
    q.head = (q.head + 1) % MAXNUM;
    return e;
}
/****************队列是否为空***********************/
int Emptyqueue(Queue& q)
{
    if (q.rear == q.head)
        return 0;
    else
        return 1;
}






int locate(ALGRAPH G, string n)
{
    int i;
    for (i = 0;i < G.vexnum;++i)
    {
        if (G.ver[i].data == n)
            return i;

    }
    return -1;
}
void created(ALGRAPH& G)
{
    //邻接表表示无向图
    int i, k, j;
    string v1, v2;
    Anode* p1, * p2;
    cout << "输入顶点数和边数:";
    cin >> G.vexnum >> G.arcnum;
    cout << "输入顶点信息:" << endl;
    for (i = 0;i < G.vexnum;i++)//构造表头
    {
        cout << "第" << i + 1 << "个顶点:" << endl;
        cin >> G.ver[i].data;
        G.ver[i].first = NULL;
    }
    for (k = 0;k < G.arcnum;k++)//构造邻接表
    {
        cout << "请输入边(Vi,Vj)上的顶点信息:" << endl;
        cin >> v1 >> v2;
        i = locate(G, v1);
        j = locate(G, v2);
        p1 = new Anode;
        p1->num = j;
        p1->next = G.ver[i].first;
        G.ver[i].first = p1;
        p2 = new Anode;
        p2->num = i;
        p2->next = G.ver[j].first;
        G.ver[j].first = p2;
    }
}
void prain(ALGRAPH G)
{
    Anode* p;
    cout << "所建立的邻接表如下所示:" << endl;
    for (int i = 0;i < G.vexnum;i++)
    {
        cout << G.ver[i].data;
        p = G.ver[i].first;
        while (p != NULL)
        {
            cout << "-->" << p->num;
            p = p->next;

        }
        cout << endl;


    }




}

bool  Dvisited[50];
bool  Bvisited[50];
void DFST(ALGRAPH G)
{
    int i;

    for (i = 0;i < G.vexnum;i++)
        Dvisited[i] = false;

}




//深度遍历
void DFS(ALGRAPH G, int v)
{

    int w;
    Anode* p;


    cout << G.ver[v].data << "->";
    Dvisited[v] = true;//访问标志数组
    p = G.ver[v].first;
    while (p != NULL)
    {
        w = p->num;
        if (Dvisited[w] == false)
            DFS(G, w);
        p = p->next;

    }


}

//广度遍历
void BFS(ALGRAPH G, int v)
{
    int i;
    for (i = 0;i < G.vexnum;i++)
        Bvisited[i] = false;
    Anode* p;

    int u, w;
    //创建队列
    Queue q;
    //初始化一个队列,队列长度为结点个数+1
    Initqueue(q);
    Bvisited[v] = 1;
    //只要结点未被访问就访问后将其存进队列里面
    Insertqueue(q, v);
    while (Emptyqueue(q) != 0)
    {
        u = Deletequeue(q);
        cout << G.ver[u].data << "->"; //输出广度优先访问顺序
        p = G.ver[u].first;
        while (p != NULL)            //p非空说明顶点u存在邻接点
        {
            w = p->num;
            if (Bvisited[w] == false)   //说明w未被访问
            {
                Bvisited[w] = true;     //访问
                Insertqueue(q, w); //进队列
            }
            p = p->next; //p指向顶点u的下一个邻接点
        }
    }
}




int main()
{
    int v = 0;
    ALGRAPH  G;
    created(G);
    prain(G);
    cout << "深度遍历搜索为:" << endl;
    DFS(G, v);
    cout << endl;
    cout << "广度遍历搜索为:" << endl;
    BFS(G, v);


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值