图的邻接表存储与遍历

    #include<stdio.h>
    #include<stdlib.h>
    #include<queue>      //调用STL队列库函数
    #include<stack>      //调用STL栈库函数
    #define max 20
    using namespace std;
    int visited[max]={0};
    typedef struct Arcnode{
        int adj;
        struct Arcnode *nextarc;
    }Arcnode;
    typedef struct Vnode {
        int data;
        Arcnode *firstarc;
    }Vnode,Adjlist[max];
    typedef struct{
        Adjlist vertices;
        int vex,arc;
    }Algraph;
    int Locatevex(Algraph &G,int v)
        {
          int i,k=-1;
               for(i=0;i<G.vex;i++)
                    if(v==G.vertices[i].data)
                    {
                       k=i;
                       break;
                    }
              return k;
        }
    void CreateAdjList(Algraph &G)
    {                                      //创建图的邻接表
        int i,j,k;
        int v1,v2;
        Arcnode *p,*s;
        printf ("输入图的顶点数与边数:\n");
        scanf("%d%d",&G.vex,&G.arc);
        printf ("输入顶点信息:\n");
        for( i=0;i<G.vex;++i)   ///初始化头结点
             {
                 scanf("%d",&G.vertices[i].data);
                 G.vertices[i].firstarc = NULL;
             }
             printf ("输入一边依附的两个顶点:\n");
        for( k=0;k<G.arc;++k)
            {     //构造邻接表
              scanf("%d%d",&v1,&v2);
              i=Locatevex(G,v1);
              j=Locatevex(G,v2);
              s=(Arcnode*)malloc(sizeof(Arcnode));
              s->adj=j;
              s->nextarc=NULL;
              p=G.vertices[i].firstarc;
              if(!p)
                G.vertices[i].firstarc=s;
              else
                {
                  while(p->nextarc)
                  p=p->nextarc;
                  p->nextarc=s;
                }
          }
    }
    void DFS(Algraph g,int m)
    {
        int v,w;
        Arcnode *p;
        v=Locatevex(g,m);
        visited[v]=1;
        printf("%d ",g.vertices[v].data);
        p=g.vertices[v].firstarc;
        while(p!=NULL)
        {
            w=p->adj;
            if(visited[w]==0)
                DFS(g,g.vertices[w].data);
            p=p->nextarc;
        }

    }
    void DFSTravel(Algraph g)
    {                                  //深度优先递归遍历
        int v;
        for(v=0;v<g.vex;v++)
            visited[v]=0;
        for(v=0;v<g.vex;v++)
        {
            if(visited[v]==0)
                DFS(g,g.vertices[v].data);
        }
    }
    void DFSTravel1(Algraph g)
    {                             //深度优先非递归遍历
        stack<int>s;
        Arcnode *p;
        int i;
        for (i=0;i<g.vex;i++)
            visited[i]=0;
        for (i=0;i<g.vex;i++)
        {
            if (!visited[i])
            {
                printf ("%d ",g.vertices[i].data);
                visited[i]=1;
                s.push(i);
                while (!s.empty())
                {
                    p=g.vertices[s.top()].firstarc;
                    while (p)
                    {
                        if (!visited[p->adj])
                        {
                            printf ("%d ",g.vertices[p->adj].data);
                            visited[p->adj]=1;
                            s.push(p->adj);
                            p=g.vertices[s.top()].firstarc;
                        }
                        else
                            p=p->nextarc;
                    }
                    s.pop();
                }
            }
        }
    }
    void BFSTravel (Algraph g)
    {                            //广度优先遍历
        queue<int>q;
        Arcnode *p;
        int s;
        int i;
        for (i=0;i<g.vex;i++)
            visited[i]=0;
         for (i=0;i<g.vex;i++)
         {
                if (!visited[i])
               {
                   visited[i]=1;
                   printf ("%d ",g.vertices[i].data);
                   q.push(i);
                   while (!q.empty())
                   {
                       s=q.front();
                       p=g.vertices[s].firstarc;
                       while (p)
                       {
                         if(!visited[p->adj])
                           {
                             visited[p->adj]=1;
                             printf ("%d ",g.vertices[p->adj].data);
                             q.push(p->adj);
                           }
                          p=p->nextarc;
                       }
                       q.pop();
                    }
               }
        }
    }
    int main ()
    {
        Algraph a;
        CreateAdjList (a);
        printf ("深度优先递归遍历:\n");
        DFSTravel(a);
        printf ("\n");
        printf ("广度优先遍历:\n");
        BFSTravel(a);
        printf ("\n");
        printf ("深度优先非递归遍历:\n");
        DFSTravel1(a);
        return 0;
    }
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/kavu1/article/details/49622815
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞