图的邻接表存储及其遍历(使用模板)

#include <iostream>
#include <STRING>
using namespace std;
const int MaxSize = 10;     //图最多10个顶点数
//邻接表存储结构
//边表节点
struct ArcNode
{
	int adjvex;          //邻接点域:节点在数组中的索引,0、1、2、3...
	ArcNode *next;	//同级子节点
};

//顶点表节点
template <class DataType>
struct VertexNode
{
	DataType vertex;
	ArcNode *firstEdge; //第一子节点	
};

template <class DataType>
class ALGraph
{
public: 
	ALGraph(DataType a[], int n, int e);
	~ALGraph();
	void DFSTraverse(int v);
	void BFSTraverse(int v);
	void print();
private:
	VertexNode<DataType> adjlist[MaxSize];	//顶点数组
	int vertexNum, arcNum;   //顶点数,边数
	int visited[MaxSize], Q[MaxSize];
	int front, rear;

};
/************************************************************************/
/* 关于队列
								5						队尾
							v4	4
		|		rear ->	    v3	3   <- 队列尾元素
		|					v2	2
		|					v1  1	<- 队列首元素
       ↓		front ->  	v0	0						队头
入队Q[++rear] = val;	对循环队列 rear = (++rear)/MaxSize; Q[rear] = val;
出队Q[++front] = val;	对循环队列 front = (++front)/MaxSize; Q[front] = val;
                                                                     */
/************************************************************************/

template <class DataType>
ALGraph<DataType>::ALGraph(DataType a[], int n, int e)
{
	vertexNum = n;
	arcNum = e;
	for (int i = 0; i < vertexNum ; i++)
	{
		adjlist[i].firstEdge = NULL;
		adjlist[i].vertex = a[i];
		visited[i] = 0;
	}
	//初始化边
	for (int k = 0; k < arcNum ; k++)
	{
		int i = 0, j = 0;
		cin >>i>>j;
		//将第j个节点放到i节点对应的报表的表头
		ArcNode *s = new ArcNode;
		s->adjvex = j;
		s->next = adjlist[i].firstEdge;
		adjlist[i].firstEdge = s;

		//将第i个节点放到j节点对应的报表的表头
		ArcNode *s2 = new ArcNode;
		s2->adjvex = i;
		s2->next = adjlist[j].firstEdge;
		adjlist[j].firstEdge = s2;
	}
}

template <class DataType>
void ALGraph<DataType>::print()
{
	for (int i = 0; i < vertexNum ; i++)
	{
		cout << i << " -> ";
		ArcNode *s = adjlist[i].firstEdge;
		while (s != NULL)
		{
			cout << s->adjvex << "->";
			s = s->next;
		}
		cout <<endl;
	}
}

template <class DataType>
void ALGraph<DataType>::DFSTraverse(int v)
{
	visited[v] = 1;
	ArcNode *s = adjlist[v].firstEdge;
	while(s != NULL)
	{
		int j = s->adjvex;
		if (visited[j] == 0) {DFSTraverse(j);}
		s = s->next;
	}
	cout << adjlist[v].vertex <<"->";
}

template <class DataType>
void ALGraph<DataType>::BFSTraverse(int v)
{
	cout << adjlist[v].vertex <<"->";
	visited[v] = 1;
	front = rear = -1;
	Q[++rear] = v;	//根节点入队
	while(front != rear)
	{
		v = Q[++front];
		ArcNode *s = adjlist[v].firstEdge;
		while(s != NULL){
			int j = s->adjvex;
			if (visited[j] == 0)
			{
				cout << adjlist[j].vertex <<"->" ;
				visited[j] = 1;
				Q[++rear] = j;
			}
			s = s->next;
		}
	}
}

int main()
{
	int a[6] = {0, 1, 2, 3, 4, 5};
	ALGraph<int> * test = new ALGraph<int>(a, 6, 6);
	test->print();
	int root;
	cin >> root;
	test->DFSTraverse(0);
	test->BFSTraverse(0);


	return 0;
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/u011450367/article/details/39829665
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞