图深度优先搜索DFS(邻接表)C/C++

//只简单写了一个ADT(不知道该不该这么说,嘻嘻嘻)

//采用的是邻接表法,图片来源于网络。

#include <bits/stdc++.h>
using namespace std;

typedef char VertexType;
typedef int EdgeType;
typedef struct EdgeNode{
    int adjvex;
    int weight;
    EdgeNode *nextedge;
}EdgeNode;

typedef struct VNode{
    int visited;
    VertexType data;
    EdgeNode *firstedge;
}VNode;

struct ALGraphStruct{
    VNode vexs[100];
    int vertexnum;
    int edgenum;
}; 

typedef struct ALGraphStruct *ALGraph;

ALGraph CreateALGraph()
{
    ALGraph g=(ALGraph)malloc(sizeof(struct ALGraphStruct));
    g->vertexnum=0;
    g->edgenum=0;
    return g; 
}

void DFS(ALGraph g,int k)
{
    EdgeNode *p;
    g->vexs[k].visited=1;
    for(p=g->vexs[k].firstedge;p!=NULL;p=p->nextedge)
        if(g->vexs[p->adjvex].visited==0)
            DFS(g,p->adjvex);
}
 

猜你喜欢

转载自blog.csdn.net/linyuan703/article/details/81287963