1个回答

`````` #include <iostream>
#include <string>

using namespace std;

#define MAXLEN 10

struct Node
{
int data;
Node *next;
};

{
int count;
string name;
};

struct Graph
{
int vexnum;
int arcnum;
};

int findIndex(Graph &G, string name)
{
int index = -1;

for (int i = 0; i < G.vexnum; i++)
{
{
index = i;
break;
}
}

if (index == -1)
cout << "error" << endl;

return index;
}

void constructGraph(Graph &G)
{
cout << "construct graph yooo" << endl;
cout << "enter vexnum" << endl;
cin >> G.vexnum;

string array[] = {"v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"};
const int size = sizeof array / sizeof *array;

for (int i = 0; i < G.vexnum; i++)
{
}

string leftName;
string rightName;

cout << "enter a pair" << endl;
cin >> leftName >> rightName;
while (leftName != "end" && rightName != "end")
{
int leftIndex = findIndex(G, leftName);
int rightIndex = findIndex(G, rightName);

Node *node = new Node;
node->data = rightIndex;
node->next = NULL;

cout << "enter a pair" << endl;
cin >> leftName >> rightName;
}
}

bool flag[MAXLEN];

void DFSTranverse(Graph &G, int num)
{
cout << G.link[num].name << " ";
flag[num] = true;

{
if (!flag[index]) //判断下是否已经遍历过
DFSTranverse(G, index); //注意，这里是递归
}
}

void main()
{
Graph G;
constructGraph(G); //生成一个图
for (int i = 0; i < MAXLEN; i++)
flag[i] = false; //初始化，默认所有节点都没有遍历过
DFSTranverse(G, 0); //搜索，第二个参数是从哪个节点寻找
}
``````