无向图的邻接表形式--DFS+BFS(前序+层次遍历)

邻接表

//图的邻接表结构

const int DefaultVertices = 30;//默认最大顶点数

//边结点,下一个顶点的位置
template<class T,class E>//T为顶点名字
struct Edge {
	int dest;     //边的另一个顶点的位置
	E cost;     //边上的权值
	Edge<T, E>* link;            //这个点的下一条边
	Edge() { dest = -1; cost = 0; link = NULL; }
	Edge(int num, E weight) { dest = num; cost = weight; link = NULL; }
};

//顶点
template<class T,class E>//T为数据
struct Vertex {
	T data;                  //顶点名字
	Edge<T,E>* adj;           //边链表的头指针
	Vertex() {};
};

//图
template<class T, class E>         //T为顶点名字
class Graph
{
public:
	int maxVertices;               //图中最大顶点数
	int numEdges;                 //当前边数
	int numVertices;              //当前顶点数
	bool* visited;                // 成员属性 辅助数组
	Vertex<T,E>* NodeTable;       //顶点表(各边链表的头指针)
	Graph(int sz);
	~Graph();
	int getVertexPos(T vetrex) {//给出顶点vetrex在图中的位置,可以改成散列的方式
		for (int i = 0; i < numVertices; i++) {
			if (NodeTable[i].data == Vertex)return i;
		}
		return -1;
	}
	void CreateNodeTable();
	void DFS(int v);
	void BFS();
};

template<class T, class E>
Graph<T,E>::Graph(int sz ) {//初始化
	maxVertices = sz; numVertices = 0; numEdges = 0;
	NodeTable = new Vertex<T,E>[maxVertices];//创建顶点表数组
	visited = new bool[sz];
	int i;
	cout << " 请输入结点数 :" << endl;
	cin >> numVertices;
	if (NodeTable == NULL||visited == NULL)
	{
		cerr << "存储分配错误!" << endl;
		exit(0);
	}
	for (i = 0; i < maxVertices; i++) {
		NodeTable[i].adj = NULL;//边链表头指针赋空
	}
	for (i = 0; i < numVertices; i++)
	{
		visited[i] = false;
	}
	CreateNodeTable();
}

template<class T, class E>
Graph<T, E>::~Graph() {
	Edge<T, E>* p;
	for (int i = 0; i < numVertices; i++)
	{
		p = NodeTable[i].adj; //保留头结点
		while (p != 0)
		{
			NodeTable[i].adj = p->link;
			delete p;
			p = NodeTable[i].adj;
		}
	}
}

template<class T, class E>
void Graph<T, E>::CreateNodeTable()
{
	int n=0, j=0, i=0, m=0;
	Edge<T, E>* p;
	cout << "输入结点个数: " << endl;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		NodeTable[i].adj = 0;
		cout << "输入该结点的值" << endl ;
		cin >> NodeTable[i].data;
		cout << "输入相邻的结点个数 : " << endl;
		cin >> m; //建立的链表的长度
		for (j = 0; j < m; j++)
		{
			p = new Edge<T, E>;
			cout << "输入连接点的 数组下标号: " << endl;
			cin >> p->dest;  //输入连接点的数组下标号
			//建链
			p->link = NodeTable[i].adj;
			NodeTable[i].adj = p;
		}
	}
}

//遍历递归,需要参数v的
//DFS=====类似前序遍历,就可以看成前序遍历(根左右)
template<class T, class E>
void Graph<T, E>::DFS(int v)                   //v表示起点
{
	Edge<T, E> *p;
	visited[v] = true;
	cout << NodeTable[v].data<<" -> "; 
	p = NodeTable[v].adj;
	while (p != 0)
	{
		if (!visited[p->dest])
		DFS(p->dest);//先把一个点的路走到头, 再找其他点作为起点
		p = p->link;
	}
}


//BFS====层次遍历(队列作为辅助结构)
//不需要初始值
template<class T, class E>
void Graph<T, E>::BFS()
{
	int v=0;  //根结点的下标
	Edge<T, E>* p;
	queue<int> qu;//定义一个队列
	cout << "输入一个出发点的位置:" << endl;
	cin >> v; //
	qu.push(v);
	visited[v] = true; //这里表示入列
	while (!qu.empty())
	{
		v = qu.front(); //取队头元素
		qu.pop();      //弹出队头元素
		cout << NodeTable[v].data<<" -> ";
		p = NodeTable[v].adj;
		//找邻接点
		while (p != 0)
		{
			qu.push(p->dest);
			p = p->link;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值