#include <iostream> #include <fstream> #define MaxVexter 250 using namespace std; ifstream cin1("e://graphlist.txt"); /*测试数据如下*/ /* 8 9ABCDEFGH 0 1 0 2 1 3 1 4 3 7 4 7 2 5 2 6 5 6 */ int visited[MaxVexter]; struct ArcNode { int adjvex; ArcNode *next; }; struct Vexter { char Date; ArcNode *firstedge; }; struct ALGraph { Vexter V[MaxVexter]; int vexnum,arcnum; }; void CreateALGraph(ALGraph *G)//创建无向图 { int i,j,k; ArcNode *p; cin1>>G->vexnum>>G->arcnum; for(i=0;i<G->vexnum;i++) { cin1>>G->V[i].Date; G->V[i].firstedge = NULL; } for (k=0;k<G->arcnum;k++) { cin1>>i>>j; p = new ArcNode; p->adjvex = j; p->next = G->V[i].firstedge; G->V[i].firstedge = p; } } void DFS(ALGraph *G,int Node)//深度优先搜索,Node指明要从Node结点开始DFS { visited[Node] = 1; cout<<G->V[Node].Date<<" "; ArcNode *p = new ArcNode; p = G->V[Node].firstedge; while (p) { Node = p->adjvex; if (!visited[Node]) { DFS(G,Node); } p = p->next; } } int main() { ALGraph *G = new ALGraph; CreateALGraph(G); DFS(G,0); cout<<endl; delete G; return 0; }