邻接链表的BFS和DFS(简明版)

代码:

#include<iostream>
#include<queue>
#include<cstdlib>
#include<string>
using namespace std;
const int MaxSize=10;
int visited[MaxSize];//定义访问数组
struct ArcNode//定义边表
{
    int adjvex;//顶点编号
    ArcNode* next;//边表指针
};
struct VertexNode//定义顶点表头
{
    char vertex;//顶点值
    ArcNode* firstedge;//边表指针
};
class AlGraph//邻接链表表类
{
public:
    AlGraph(char a[],int n,int e);//构造函数(传入顶点数组,顶点个数,边的条数)
    ~AlGraph(){}//析构函数为空
    void DFS(int v);//以顶点v为起点做深度遍历
    void BFS(int v);//广度遍历
    VertexNode adjlist[MaxSize];//顶点链表头数组
    int vertexNum,arcNum;//顶点数,边数
};
AlGraph::AlGraph(char a[],int n,int e)//顶点数组,顶点个数,边的条数
{
    vertexNum=n;arcNum=e;
    int i,j,k;
    for(i=0;i<vertexNum;i++)//初始化顶点链表
    {
        adjlist[i].vertex=a[i];
        adjlist[i].firstedge=NULL;
    }
    cout<<"输入边所在的两个顶点:"<<endl;
    for(k=0;k<arcNum;k++)
    {
        cin>>i>>j;
        ArcNode* s=new ArcNode;s->adjvex=j;//新生成的边表j
        s->next=adjlist[i].firstedge;
        adjlist[i].firstedge=s;//将边表j插到顶点表i后
        ArcNode* t=new ArcNode;t->adjvex=i;//新生成的边表i
        t->next=adjlist[j].firstedge;
        adjlist[j].firstedge=t;//将边表i插到顶点表j后
    }
}
void AlGraph::DFS(int v)//深度遍历
{
    cout<<adjlist[v].vertex<<" ";visited[v]=1;//输出顶点表v的顶点值,标记为已访问
    ArcNode* p=adjlist[v].firstedge;
    while(p)//访问顶点表p的所有边表的顶点值
    {
        if(visited[p->adjvex]==0) DFS(p->adjvex);
        p=p->next;
    }
}
void AlGraph::BFS(int v)//广度遍历
{
    queue<int> q;
        if(!visited[v])
        {
            visited[v]=1;q.push(v);
            cout<<adjlist[v].vertex<<" ";//访问顶点i的值,标记并入队
            while(!q.empty())
            {
                int x=q.front();q.pop();//取队首元素并出队
                ArcNode* p=adjlist[x].firstedge;
                while(p)//访问边表顶点值
                {
                    if(!visited[p->adjvex])
                    {
                        visited[p->adjvex]=1;

                        cout<<adjlist[p->adjvex].vertex<<" ";

                        q.push(p->adjvex);

                    }
                    p=p->next;
                }
            }
        }
}
void printAlGraph(AlGraph&A)
{
    for(int i=0;i<4;i++)//依次遍历各顶点表
    {
        cout<<A.adjlist[i].vertex<<"----->";//打印顶点表i的值
        ArcNode* p=A.adjlist[i].firstedge;
        while(p)//打印顶点i边表的顶点值
        {
            cout<<A.adjlist[p->adjvex].vertex<<"----->";
            p=p->next;
        }
        cout<<endl;
    }
}
int main()
{
    char a[4]={'a','b','c','d'};
    AlGraph A(a,4,4);
    cout<<"打印顶点:"<<endl;
     for(int i=0;i<4;i++) cout<<a[i]<<" ";
     cout<<endl;
     cout<<"打印邻接链表:"<<endl;
    printAlGraph(A);
    cout<<endl;
    visited[MaxSize]={0};
    cout<<"DFS-------"<<endl;
    A.DFS(0);
    cout<<endl;
    for(int i=0;i<4;i++) visited[i]=0;
    cout<<"BFS-------"<<endl;
    A.BFS();

    return 0;

}

截图:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值