邻接表的实现(下)

图如下所示:



效果如下:



改进,添加一个相对于顶点的方向位置。

这里为了方便计算表示,方向用了以下表示


代码如下:

//adjlist.h
#include <iostream>

using namespace std;

#define  MAXVEX 100
#define  North      0;
#define  EastNorth  1;
#define  East       2
#define  EastSouth  3;
#define  South      7;
#define  WestSouth  6;
#define  West       5;
#define  WestNorth  4 

typedef char VertexType;
typedef int EdgeType;
typedef int Direction;


typedef struct EdgeNode
{
	int adjvex;
	EdgeType weight;
	Direction direction;
	struct EdgeNode *next;
}EdgeNode;


typedef struct VertexNode
{
	VertexType data;
	EdgeNode *firstedge;

}VertexNode, AdjList[MAXVEX];

typedef struct 
{
	AdjList adjList;
	int numNodes;
	int numEdges;
}GraphAdjList;


void create_adjlist_graph(GraphAdjList *Gp);
void show_adjlist_graph(GraphAdjList *Gp);
void destroy_adjlist_graph(GraphAdjList *Gp);
string tramsformToDirection(EdgeNode *ep);

//adjlist.cpp
#include "stdafx.h"
#include "adjlist.h"
#include <string>


string tramsformToDirection(EdgeNode *ep)
{
	string strDirection;
	switch (ep->direction)
	{
	case 0:
		strDirection = "North";
		break;
	case 1:
		strDirection ="EastNorth";
		break;
	case 2:
		strDirection ="East";
		break;
	case 3:
		strDirection ="EastSouth";
		break;
	case 4:
		strDirection ="WestNorth";
		break;
	case 5:
		strDirection ="West";
		break;
	case 6:
		strDirection ="WestSouth";
		break;
	case 7:
		strDirection ="South";
		break;

	}
	return strDirection;
}

void create_adjlist_graph(GraphAdjList *Gp)
{
	int i, j, k, w, d;   //i,j分别表示两个节点,w表示权值,k代表边数
	EdgeNode *pe;
	cout << "输入这图的顶点数和边数(空格分隔):"<< endl;
	cin >> Gp->numNodes >> Gp->numEdges;

	//设置顶点
	for (i = 0; i < Gp->numNodes; ++i)
	{
		cout << "输入顶点信息:" << endl;
		cin >> Gp->adjList[i].data;
		Gp->adjList[i].firstedge = NULL;
	}

	//建立边表
	for (k = 0; k < Gp->numEdges; ++k)
	{
		cout << "输入一边(vi, vj, w, d)的两个顶点序号i, j(空格分隔): "<< endl;
		cin >> i >> j >> w >> d;
		pe = (EdgeNode *)malloc(sizeof(EdgeNode));
		pe->adjvex = j;
		pe->weight = w;
		pe->direction = d;
		pe->next = Gp->adjList[i].firstedge;
		Gp->adjList[i].firstedge = pe;

		pe = (EdgeNode *)malloc(sizeof(EdgeNode *));
		pe->adjvex = i;
		pe->weight = w;
		pe->direction = 7-d;
		pe->next = Gp->adjList[j].firstedge;
		Gp->adjList[j].firstedge = pe;
	}

	cout << "无向图的邻接表创建成功。" << endl;
}

void show_adjlist_graph(GraphAdjList *Gp)
{
	string strDirction;
	cout << "邻接表如下:" << endl;
	if (Gp->numNodes == 0 || Gp->numEdges == 0)
	{
		cout <<"the adjlist is fail" << endl;
	}


	for (int v = 0; v < Gp->numNodes; ++v)
	{
		
		cout << "V" << v << ":";
		if (Gp->adjList[v].firstedge == NULL)
		{
			cout << "NULL" << endl;
		}
		else
		{
			EdgeNode *cur = Gp->adjList[v].firstedge;
			
			while (cur != NULL)
			{
				strDirction = tramsformToDirection(cur);
				cout << "V" << cur->adjvex << "( " <<cur->weight<<" | "<< strDirction << " )" << " 、   ";
				cur = cur->next;
			}
			cout << endl;
		}
	}
}

void destroy_adjlist_graph(GraphAdjList *Gp)
{
	if (Gp->numNodes == 0 || Gp->numEdges == 0)
	{
		return;
	}else
	{
		for (int v = 0; v < Gp->numNodes; ++v)
		{
			while (Gp->adjList[v].firstedge != NULL)
			{
				EdgeNode *cur = Gp->adjList[v].firstedge;
				Gp->adjList[v].firstedge = cur->next;
				free(cur);
			}
		}
	}
	cout <<"邻接表销毁成功。"<<endl;
}

// c++_ver.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "adjlist.h"


int _tmain(int argc, _TCHAR* argv[])
{
	GraphAdjList g;
	create_adjlist_graph(&g);
	show_adjlist_graph(&g);

	system("pause");
	return 0;
}
效果如下:




在此基础上再改进:

增加搜索点,进行访问。

代码如下:

//adjlist.cpp
#include "stdafx.h"
#include "adjlist.h"
#include <string>


string tramsformToDirection(EdgeNode *ep)
{
	string strDirection;
	switch (ep->direction)
	{
	case 0:
		strDirection = "North";
		break;
	case 1:
		strDirection ="EastNorth";
		break;
	case 2:
		strDirection ="East";
		break;
	case 3:
		strDirection ="EastSouth";
		break;
	case 4:
		strDirection ="WestNorth";
		break;
	case 5:
		strDirection ="West";
		break;
	case 6:
		strDirection ="WestSouth";
		break;
	case 7:
		strDirection ="South";
		break;

	}
	return strDirection;
}

void create_adjlist_graph(GraphAdjList *Gp)
{
	int i, j, k, w, d;   //i,j分别表示两个节点,w表示权值,k代表边数
	EdgeNode *pe;
	cout << "输入这图的顶点数和边数(空格分隔):"<< endl;
	cin >> Gp->numNodes >> Gp->numEdges;

	//设置顶点
	for (i = 0; i < Gp->numNodes; ++i)
	{
		cout << "输入顶点信息:" << endl;
		cin >> Gp->adjList[i].data;
		Gp->adjList[i].firstedge = NULL;
	}

	//建立边表
	for (k = 0; k < Gp->numEdges; ++k)
	{
		cout << "输入一边(vi, vj, w, d)的两个顶点序号i, j(空格分隔): "<< endl;
		cin >> i >> j >> w >> d;
		pe = (EdgeNode *)malloc(sizeof(EdgeNode));
		pe->adjvex = j;
		pe->weight = w;
		pe->direction = d;
		pe->next = Gp->adjList[i].firstedge;
		Gp->adjList[i].firstedge = pe;

		pe = (EdgeNode *)malloc(sizeof(EdgeNode *));
		pe->adjvex = i;
		pe->weight = w;
		pe->direction = 7-d;
		pe->next = Gp->adjList[j].firstedge;
		Gp->adjList[j].firstedge = pe;
	}

	cout << "无向图的邻接表创建成功。" << endl;
}

void show_adjlist_graph(GraphAdjList *Gp)
{
	string strDirction;
	cout << "邻接表如下:" << endl;
	if (Gp->numNodes == 0 || Gp->numEdges == 0)
	{
		cout <<"the adjlist is fail" << endl;
	}


	for (int v = 0; v < Gp->numNodes; ++v)
	{
		
		cout << "V" << v << ":";
		if (Gp->adjList[v].firstedge == NULL)
		{
			cout << "NULL" << endl;
		}
		else
		{
			EdgeNode *cur = Gp->adjList[v].firstedge;
			
			while (cur != NULL)
			{
				strDirction = tramsformToDirection(cur);
				cout << "V" << cur->adjvex << "( " <<cur->weight<<" | "<< strDirction << " )" << " 、   ";
				cur = cur->next;
			}
			cout << endl;
		}
	}
}

void destroy_adjlist_graph(GraphAdjList *Gp)
{
	if (Gp->numNodes == 0 || Gp->numEdges == 0)
	{
		return;
	}else
	{
		for (int v = 0; v < Gp->numNodes; ++v)
		{
			while (Gp->adjList[v].firstedge != NULL)
			{
				EdgeNode *cur = Gp->adjList[v].firstedge;
				Gp->adjList[v].firstedge = cur->next;
				free(cur);
			}
		}
	}
	cout <<"邻接表销毁成功。"<<endl;
}

void seek_adjlist_graph(GraphAdjList *Gp)
{
	char vertex_label;
	int  vertex_sub;
	int  Dir;
	bool sign_seek = true;  //搜索到则表示目标在

	cout << "输入第一次车辆出现的位置:"<< endl;
	cin >> vertex_label;

	vertex_sub = vertex_label - 65;   //将A,B,C....转换成下标0, 1, 2.... 
	

	while (sign_seek)
	{
		cout << "车辆在摄像头"<<char(vertex_sub + 65)<<"中"<<endl;
		sign_seek = false;
		EdgeNode *cur = Gp->adjList[vertex_sub].firstedge;
		if (Gp->adjList[vertex_sub].firstedge = NULL)
		{
			cout <<"NULL"<<endl;
		}
		else
		{
			cout << "给出当前信息行驶方位:"<<endl;
			cin >> Dir;
			while (cur != NULL)
			{
				if (cur->direction == Dir)
				{
					vertex_sub = cur->adjvex;
					cout << "车往"<<char(vertex_sub + 65)<<"摄像头去了"<<endl;
					sign_seek = true;
					break;
				}
				else
				{
					cur = cur->next;
				}
				
			}
			

		}
	}

	
	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值