领接表-数据结构

领接表

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include "stdlib.h"
#include "windows.h"
#include "InterFace.h"
using namespace std;
#define OK 1
#define ERROR 0
#define M 100
typedef char VexType;

typedef struct ArcNode {
	int adjvex;
	int weigh;
	struct ArcNode *nextarc;
}ArcNode;

typedef struct VNode {
	VexType data;
	ArcNode *firstarc;
}VNode, AdjList[M];

typedef struct {
	AdjList vertices;
	int vexnum, arcnum;
}ALGraph;

int visited[M] = { 0 };

void InitGraph(ALGraph &g);
int LocateVex(ALGraph g, VexType u);//查找值为u的顶点在图中的位置
void InsertVex(ALGraph &g, VexType v);//图中插入一个值为v的顶点
void InsertArc(ALGraph &g, VexType v, VexType u,int w);//图中插入一条边
void DeleteArc(ALGraph &g, VexType v, VexType u);//删除一条边
void DeleteVex(ALGraph &g, VexType v);//删除一个顶点
void CreateUDN(ALGraph &g);//建立一个无向图
void DFS_AL(ALGraph g, VexType v);//深度遍历连通图
void BFS_AL(ALGraph g, VexType v);//广度遍历连通图

int main() {
	InterFace *F = NULL;
	F->fun();
	ALGraph g;
	CreateUDN(g);
	DeleteArc(g, 'a', 'b');
	DFS_AL(g, 'a');
	system("pause");
	return 0;
}

void InitGraph(ALGraph & g)
{
	g.vexnum = g.arcnum = 0;
}

int LocateVex(ALGraph g, VexType u)
{
	int i;
	for (i = 0; i < g.vexnum; i++)
		if (g.vertices[i].data == u)
			return i;
	return -1;
}

void InsertVex(ALGraph & g, VexType v)
{
	g.vertices[g.vexnum].data = v;
	g.vertices[g.vexnum].firstarc = NULL;
	g.vexnum++;
	return;
}

void InsertArc(ALGraph & g, VexType v, VexType u, int w)
{
	int i = LocateVex(g, v);
	int j = LocateVex(g, u);
	if (i == -1 || j == -1)
		return;
	ArcNode *p;
	p = new ArcNode;
	p->adjvex = j;
	p->weigh = w;
	p->nextarc = g.vertices[i].firstarc;
	g.vertices[i].firstarc = p;

	p = new ArcNode;
	p->adjvex = i;
	p->weigh = w;
	p->nextarc = g.vertices[j].firstarc;
	g.vertices[j].firstarc = p;

	g.arcnum++;
}

void DeleteArc(ALGraph & g, VexType v, VexType u)
{
	int i = LocateVex(g, v);
	int j = LocateVex(g, u);
	if (i == -1 || j == -1)
		return;
	ArcNode *p, *q;
	
	//删除从v出发邻接顶点为u的边结点
	p = g.vertices[i].firstarc;
	if (p->adjvex == j) {
		g.vertices[i].firstarc = p->nextarc;
		delete p;
	}
	else {
		while (p->nextarc->adjvex != i)
			p = p->nextarc;
		q = p->nextarc;
		p->nextarc = q->nextarc;
		delete q;
	}

	//删除从u出发邻接顶点为v的边结点
	p = g.vertices[j].firstarc;
	if (p->adjvex == i) {
		g.vertices[j].firstarc = p->nextarc;
		delete p;
	}
	else {
		while (p->nextarc->adjvex != i)
			p = p->nextarc;
		q = p->nextarc;
		p->nextarc = q->nextarc;
		delete q;
	}
	g.arcnum--;
}

void DeleteVex(ALGraph & g, VexType v)
{
	int i, j;
	ArcNode *p;
	VexType u;
	i = LocateVex(g, v);
	if (i == -1)
		return;
	p = g.vertices[i].firstarc;
	while (p) {
		j = p->adjvex;
		u = g.vertices[j].data;
		DeleteArc(g, v, u);
		p = g.vertices[i].firstarc;
	}
	g.vexnum--;
}

void CreateUDN(ALGraph & g)
{
	int vexnum, arcnum, i, w;
	VexType v, u;
	InitGraph(g);
	cout << "请输入图的顶点及边数:" << endl;
	cin >> vexnum >> arcnum;
	cout << "请输入" << vexnum << "个顶点数据:" << endl;
	for (i = 0; i < vexnum; i++) {
		cin >> v;
		InsertVex(g, v);
	}
	for (i = 0; i < arcnum; i++) {
		cout << "请输入第" << i + 1 << "条边关联的顶点及边上的权重:" << endl;
		cin >> v >> u >> w;
		InsertArc(g, v, u, w);
	}
}

void DFS_AL(ALGraph g, VexType v)
{
	int i = LocateVex(g, v);
	if (i == -1)
		return;
	ArcNode *p;
	cout << v << " ";
	visited[i] = 1;
	p = g.vertices[i].firstarc;
	while (p) {
		int w = p->adjvex;
		if (!visited[w])
			DFS_AL(g, g.vertices[w].data);
		p = p->nextarc;
	}
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值