No not entitled to FIG recursion represented by adjacency list dfs

Compared    adjacent table LINk  

Improving data structure: node names ElemType

 

  Above:

 

 

   The code:

  . 1  // VS, 2015
   2  // adjacency list undirected without right 
  . 3  
  . 4  
  . 5 #include <the iostream>
   . 6 #include <Stack>
   . 7  
  . 8  the using  namespace STD;
   . 9  #define MAX 10
 10  
. 11 typedef int elemType;             // node types are labeled
 12  // arc-node 
13 is typedef struct ArcNode {
 14      elemType name;                 // with vertices adjacent arc name 
15      struct ArcNode * Next;         //Next sibling arc 
16  } ArcNode;
 . 17  // vertex information 
18 is typedef struct vnode {
 . 19      elemType name;                 // vertices are marked name 
20 is      ArcNode * firstarc;             // first vertex connected arcs (insensitive about) 
21 is  } vnode , AdjList [MAX];
 22 is  // adjacency table 
23 is typedef struct {
 24      AdjList vertinfo;             // the hash table 
25      int vexnum;                     // number of vertices 
26 is      int arcnum;                     //Arc number 
27  } AdjGraph;
 28  
29  
30  int visited [MAX];
 31 is  void dfs_byecursion (AdjGraph G, elemType stapos)
 32  {
 33 is      // ------- enhance the robustness, a first main detecting whether stapos FIG. in ------ 
34 is  
35      BOOL isIn = to false ;         // determines whether stapos nodes in FIG. 
36      int POS = 0 ;             // record the nodes in the index vertinfo 
37 [      for ( int I = 0 ; I < G.vexnum; I ++ )
 38 is          IF (== staposG.vertinfo [I] .name) {
 39              isIn = to true ;
 40              POS = I;
 41 is              BREAK ;
 42 is          }
 43 is      IF ! ( IsIn)
 44 is          return ;                 // not the drawing, out of 
45  
46 is      visited [POS] = . 1 ;         // after if in the figures, the operation of this node is labeled
 47                              @ Thereafter pos useless, for recording a position adjacent to the point vertinfo 
48      COUT << G.vertinfo [pos] .name << ' \ T ' ;
 49          / * 
50             function (); // where only the output
 51          * / 
52      // ----------------------------------- ------------------- 
53 is      
54 is      ArcNode * TEMP;             // temporary arc pointer stapos node adjacent point 
55      TEMP = G.vertinfo [POS] .firstarc;
 56 is      the while (! TEMP = NULL)
 57 is      {
 58          for ( int I = 0 ; I <G.vexnum; I ++ ) {
 59              IF (temp-> name == G.vertinfo [I] .name)
 60              {
 61 is                  POS = i;
62 is                  BREAK ;
 63 is              }
 64          }
 65          // temp-> name represents the name of the node adjacent to the point, is determined to be found will have visited the subscripts vertinfo
 66  
67          // ---------- ---------------------------------- 
68          IF (! visited [POS])
 69              dfs_byecursion (G, temp- > name);
 70          TEMP = temp-> Next;
 71 is      }
 72  }
 73 is  void travelallnodes_dfs_byecrusion (AdjGraph G) {
 74      COUT << " DFS All Nodes: " << endl;
 75     int partnum = 1;
 76     for (int i = 0; i < G.vexnum; i++) {
 77         if (!visited[i]) {
 78             cout << "part " << partnum++ << " :" << endl;
 79             dfs_byecursion(G, G.vertinfo[i].name);
 80             cout << endl;
 81         }
 82     }
 83     cout << "-----dfs all nodes over!" <<endl;
84  }
 85  int main ()
 86  {
 87      AdjGraph ag;
88  
89      ag.vexnum = 6 , ag.arcnum = 5 ;
90      ArcNode acn [ 10 ];
91      ag.vertinfo [ 0 ] .name = 1 ;
92      acn [ 0 ] .name = 2 , acn [ 1 ] .name = 3 , acn [ 2 ] .name = 5 ;
93      ag.vertinfo [ 0 ] .firstarc = & acn [ 0];
 94     acn[0].next = &acn[1], acn[1].next = &acn[2], acn[2].next = NULL;
 95     
 96     ag.vertinfo[1].name = 2;
 97     acn[3].name = 1, acn[4].name = 4;
 98     ag.vertinfo[1].firstarc = &acn[3];
 99     acn[3].next = &acn[4], acn[4].next = NULL;
100     
101     ag.vertinfo[2].name = 3;
102     acn[5].name = 1, acn[6].name = 5;
103     acn[5].next = &acn[6], acn[6].next = NULL;
104     ag.vertinfo[2].firstarc = &acn[5];
105     
106     ag.vertinfo[3].name = 4;
107     acn[7].name = 2, acn[7].next = NULL;
108     ag.vertinfo[3].firstarc = &acn[7];
109     
110     ag.vertinfo[4].name = 5;
111     acn[8].name = 3, acn[9].name = 1;
112     acn[8].next = &acn[9], acn[9].next = NULL;
113     ag.vertinfo[4].firstarc = &acn[8];
114     
115     ag.vertinfo[5].name = 5;
1 16      ag.vertinfo [ . 5 ] .firstarc = NULL;
 117  
1 18      for ( int I = 0 ; I <ag.vexnum; I ++ )
 119          visited [I] = 0 ;         // visited global initialization methods require the use of a recursive array
 120      
121      // second parameter: node names 
122      dfs_byecursion (AG, 2 );
 123      travelallnodes_dfs_byecrusion (AG);
 124  
125      return  0 ;
 126 }

 

Guess you like

Origin www.cnblogs.com/guoyujiang/p/11967936.html