用C++写一个简单的邻接表~

邻接表是图的一种最主要存储结构,用来描述图上的每一个点。
对图的每个顶点建立一个容器(n个顶点建立n个容器),第i个容器中的结点包含顶点Vi的所有邻接顶点。
实际上我们常用的邻接矩阵就是一种未离散化每个点的边集的邻接表。

首先是邻接表的简历 采用插入顶点的方式将顶点一一插入
再依次插入边

引入自己所写的一个队列 实际上引入系统的队列就可以了

#include <iostream>
using namespace std;
#include <queue>

#define SIZE 10 //这里为了方便直接将SIZE写死了
struct Edge
{
	Edge(int v):destvalue(v),link(NULL){}
	int destvalue;
	Edge *link;
};
struct Vertex
{
	Vertex():list(NULL){}
	char data;
	Edge *list;
};
class GraphLink
{
public:
	GraphLink()
	{
		MaxVertex = SIZE;
		NumVertex = NumEdge = 0;
		VertexTable = new Vertex[MaxVertex];
	}

	~GraphLink()
	{}

	void InsertVertex(char v)
	{
		if(NumVertex >= MaxVertex)
			return;
		VertexTable[NumVertex++].data = v;
	}
	int GetVertexI(char v)
	{
		for(int i = 0;i<NumVertex;i++)
		{
			if(VertexTable[i].data == v)
				return i;
		}
		return -1;
	}
	void InsertEdge(char v1,char v2)
	{
		int p1 = GetVertexI(v1);
		int p2 = GetVertexI(v2);
		if(p1 == -1 || p2 == -1)
			return;
		Edge *ed = new Edge(p2);
		ed->link = VertexTable[p1].list;
		VertexTable[p1].list = ed;
		
		ed = new Edge(p1);
		ed->link = VertexTable[p2].list;
		VertexTable[p2].list = ed;
		NumEdge++;
	}
	void Show()
	{
		Edge *ed = NULL;
		for(int i = 0;i<NumVertex;i++)
		{
			cout<<i<<":"<<VertexTable[i].data<<"->";
			ed = VertexTable[i].list;
			while(ed)
			{
				cout<<ed->destvalue<<"->";
				ed = ed->link;
			}
			cout<<"nul"<<endl;
		}
	}
	void BFS(char value)//广度优先遍历
	{
		queue<int> q;
		int v = GetVertexI(value);
		bool *visited = new bool[NumVertex];
		Edge *ed = NULL;
		int w;
		for(int i = 0;i<NumVertex;i++)
			visited[i] = false;
		if(v == -1)
			return;
		cout<<value<<" ";
		visited[v] = true;
		q.push(v);
		while(!q.empty())
		{
			v = q.front();
			q.pop();
			ed = VertexTable[v].list;
			while(ed)
			{
				w = ed->destvalue;
				if(!visited[w])
				{
					cout<<VertexTable[w].data<<" ";
					q.push(w);
					visited[w] = true;
				}
				ed = ed->link;
			}
		}
	}
	void DFS(char v)//深度优先遍历接口
	{
		bool *visited = new bool[NumVertex];
		for(int i = 0;i<NumVertex;i++)
			visited[i] = false;
		DFS(GetVertexI(v),visited);
		delete [] visited;
		visited = NULL;
	}
	char GetVertex(int v)
	{
		return VertexTable[v].data;
	}
	void DFS(int v,bool *visited) //深度优先遍历
	{
		cout<<GetVertex(v)<<" ";
		visited[v] = true;
		int w = GetFirstNeighbor(v);
		while(w != -1)
		{
			if(!visited[w])
			{
				DFS(w,visited);
			}
			w = GetNextNeighbor(v,w);
		}
	}
	int GetFirstNeighbor(int v) //v的第一个邻接点
	{
		if(v == -1)
			return -1;
		Edge *p = VertexTable[v].list;
		if(p)
			return p->destvalue;
		return -1;
	}
	int GetNextNeighbor(int v,int w)//v的邻接点w的下一个邻接点
	{
		if(v == -1 || w== -1)
			return -1;
		Edge *p = VertexTable[v].list;
		while(p && p->destvalue != w)
			p = p->link;
		if(p && p->link)
			return p->link->destvalue;
		return -1;
	}
private:
	int MaxVertex;
	int NumVertex;
	int NumEdge;
	Vertex *VertexTable;
};
代码上对析构函数还未进行编写

void main()
{
	GraphLink gh;
	gh.InsertVertex('a');
	gh.InsertVertex('b');
	gh.InsertVertex('c');
	gh.InsertVertex('d');
	gh.InsertVertex('e');

	gh.InsertEdge('a','b');
	gh.InsertEdge('a','e');
	gh.InsertEdge('b','c');
	gh.InsertEdge('b','d');
	gh.InsertEdge('c','d');
	gh.InsertEdge('c','e');
	gh.InsertEdge('e','d');

	gh.Show();
	gh.BFS('c');
	cout<<endl;
	gh.DFS('b');
	cout<<endl;
}

主函数测试结果如下

测试环境为VS2010.
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值