#pragma once
#include "pch.h"
namespace map {
typedef class map10listarc {
public:
int tail, head;
int info;
map10listarc *h; map10listarc *t;
}arc;
typedef class map10listvertex
{
public:
char data;
arc *fin, *fout;
int node;
}ver;
class map10list:public map10listarc ,public map10listvertex{
public:
void init(map10list&);
int locate(map10list&, char );
void DFSv0(map10list&,int);
void DFS(map10list&);
private:
ver vertex[max];
bool DFSv[max];
int vernum, arcnum;
};
}
#include "pch.h"
#include "图.h"
#include "队列.h"
using namespace map;
int map10list::locate(map10list &l,char v)
{
int x = 0;
while (l.vertex[x].data != v && x < l.vernum)
{
x++;
}
return x;
}
void map10list::init(map10list&l)
{
cout << "输入有向图的顶点数" << endl;
cin >> l.vernum;
cout << "输入有向图的顶点资料" << endl;
for (int i = 0; i < l.vernum; i++)
{
cin >> l.vertex[i].data;
l.vertex[i].node = i;
l.vertex[i].fin = NULL;
l.vertex[i].fout = NULL;
}
cout << "输入有向图的弧数" << endl;
cin >> l.arcnum;
cout << "输入有向图的弧的资料" << endl;
char vt, vh;
int v1, v2;
for (int i = 0; i < l.arcnum; i++)
{
cout << "输入第" << i << "边的头" << endl;
cin >> vt;
cout << "输入第" << i << "边的尾" << endl;
cin >> vh;
v1 = locate(l, vt);
v2 = locate(l, vh);
arc *a = new arc;
cout << "输入第"<<i<<"边的权值" << endl;
cin >> a->info;
a->tail = v1;
a->head = v2;
a->h = l.vertex[v2].fin;
a->t = l.vertex[v1].fout;
l.vertex[v2].fin = l.vertex[v1].fout = a;
}
};
void map10list::DFS(map10list&l)
{
for (int i = 0; i < l.vernum; i++)
l.DFSv[i] = false;
for (int i = 0; i < l.vernum; i++) {
if (!DFSv[i])
DFSv0(l, i);
}
};
void map10list::DFSv0(map10list&l,int i)
{
l.DFSv[i] = true;
cout << "顶点的数据是" << l.vertex[i].data<< endl;
arc *DFStemp = l.vertex[i].fout;
while (DFStemp)
{
if (!DFSv[DFStemp->head])
{
DFSv0(l, DFStemp->head);
}
DFStemp = DFStemp->t;
}
};