2020-10-29

OJ有向图的邻接表表示法验证程序 题目编号:516

5 7
A B C D E
0 1
0 2
0 3
1 2
1 3
2 4
3 4

A B C D E
A 3 2 1
B 3 2
C 4
D 4
E
A D E C B
A D C B E

#include<iostream>
using namespace std;
const int MaxSize = 20;
struct EdgeNode//保存边表结点其中有下一个结点的下表和指向下一个结点的指针
{
EdgeNode *next;//下一结点
};

struct VertexNode
{
char vertex;						//保存顶点
EdgeNode *firstEdge;				//指针域，指向下一个结点
};

class AlGraph
{
public:
AlGraph();
~AlGraph();
void DETraverse(int v);		//深度优先遍历
void BFTraverse(int v);		//广度优先遍历
void BFTraverse1(int v);	//如果广度遍历未全部遍历就遍历剩下的
void LinJie(int v);
void DEnoCross();			//遍历没有访问过的顶点
void show();			   //输出顶点信息
int visited[MaxSize];
int EdgeNum,VertexNum; 	   //保存顶点和边的个数
private:

};

void AlGraph::VertexShowing()
{
for(int i = 0; i < VertexNum; i++)
{
cout << adjlist[i].vertex << " ";
}
}
void AlGraph::DEnoCross()
{
for(int i = 0; i < VertexNum; i++)
{
if(visited[i] == 0)
{
DETraverse(i);				//如果没有访问过，就重新进行深度遍历
}
}
}
void AlGraph::LinJie(int v)				//输出邻接表，以及该顶点的所有邻接点序号
{
for (int i = 0; i < VertexNum; i++)
{
visited[i] = 0;
}
EdgeNode *p = NULL;
for(int i = 0;i < VertexNum; i++)
{
cout << adjlist[i].vertex << " ";
while(p != NULL)
{
cout << p->adjvex << " ";
p = p->next;
}
cout << endl;
}
}
AlGraph::AlGraph()
{
int x, y;							//用来保存边
EdgeNode *s = NULL;
cin >> VertexNum >> EdgeNum;		//输入边数和顶点数

for(int i =0;i < VertexNum; i++)	//循环输入顶点
{
visited[i] = 0;
}
for(int j = 0; j < EdgeNum; j++)	//循环输入边
{
cin >> x >> y;
s = new EdgeNode;				//将与顶点相关的边用首插法构建成一个链表
}
}

AlGraph::~AlGraph()						//循环释放顶点资源
{
EdgeNode *p = NULL, *q = NULL;
for(int i = 0; i < VertexNum; i++)
{
while(p != NULL)
{
p = p->next;
delete q;
q = p;
}
}
}

void AlGraph::DETraverse(int v)			//深度优先遍历：是通过链表来遍历
{
int j;
EdgeNode *p = NULL;
cout << adjlist[v].vertex << " ";
visited[v] = 1;
while(p != NULL)
{

if(visited[j] == 0)
{
DETraverse(j);
}
p = p->next;
}

}

void AlGraph::BFTraverse(int v)			//广度优先遍历是通过数组来实现
{
for(int i = 0; i < VertexNum; i++)
{
visited[i] = 0;
}
EdgeNode *p = NULL;
char Q[MaxSize];					//用来保存待访问的顶点
int w,j;
int rear,front;
rear = front = -1;
cout << adjlist[v].vertex << " ";
visited[v] = 1;
Q[++rear] = v;
while(rear != front)
{
w = Q[++front];
while(p != NULL)
{

if(visited[j] == 0)
{
cout << adjlist[j].vertex << " ";
visited[j] = 1;
Q[++rear] = j;
}
p = p->next;
}
}
front = rear = -1;

}
void AlGraph::BFTraverse1(int v)			//广度优先遍历是通过数组来实现
{

EdgeNode *p = NULL;
char Q[MaxSize];					//用来保存待访问的顶点
int w,j;
int rear,front;
rear = front = -1;
cout << adjlist[v].vertex << " ";
visited[v] = 1;
Q[++rear] = v;
while(rear != front)
{
w = Q[++front];
while(p != NULL)
{

if(visited[j] == 0)
{
cout << adjlist[j].vertex << " ";
visited[j] = 1;
Q[++rear] = j;
}
p = p->next;
}
}
front = rear = -1;

}

int main()
{
AlGraph A;
A.show();
cout<<endl;
A.LinJie(0);
A.DETraverse(0);
A.DEnoCross();
cout  << endl;
A.BFTraverse(0);
for(int i = 0; i < A.VertexNum; i++)
{
if(A.visited[i] == 0)
{
A.BFTraverse1(i);
//	cout<<A.visited[i];
}

}
}

01-26 2505

04-10 1669
05-10 596
05-19 614