28 邻接表:构造有权图

28 邻接表:构造有权图

作者: 冯向阳时间限制: 1S章节: DS:图

截止日期: 2022-06-30 23:55:00

问题描述 :

目的:使用C++模板设计并逐步完善图的邻接表抽象数据类型(ADT)。

内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接表ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)

(2)使用构造函数,构造一个有权图。

注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)

参考函数原型:

//构造函数构造一个有权图。6个参数的含义:图的类型、结点数、边数、结点集、边集、权集 

template<class TypeOfVer, class TypeOfEdge>

adjlist_graph<TypeOfVer, TypeOfEdge>::adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e, const TypeOfEdge w[]);

图的邻接表模板类原型参考如下:

/* 边表的结点定义 */

template<class TypeOfEdge>

struct edgeNode

{

    int data;

    TypeOfEdge weight;

    edgeNode<TypeOfEdge> *next;

    edgeNode(const int &d, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(无权图) 

    //函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面

    {

        next = ptr;

        data = d;

    }

    edgeNode(const int &d, const TypeOfEdge &w, edgeNode<TypeOfEdge> *ptr = NULL) //构造函数,用于构造其他结点(带权图) 

    //函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面

    {

        next = ptr;

        data = d;

        weight = w;

    }

    int getData(){ return data;}  //取得结点的序号(顶点集) 

    TypeOfEdge getWeight(){ return weight;}  //取得边集中对应边的权值 

    void SetLink( edgeNode<TypeOfEdge> *link ){ next = link; }  //修改结点的next域 

    void SetData( int value ){ data = value; }   //修改结点的序号(顶点集) 

    void SetWeight(TypeOfEdge value ){ weight = value; }   //修改边集中对应边的权值   

};

//图的邻接表类

template<class TypeOfVer, class TypeOfEdge>

struct verNode

{

    TypeOfVer ver;

    edgeNode<TypeOfEdge> *head;

    

    verNode(edgeNode<TypeOfEdge> *h = NULL){head = h;} 

    TypeOfVer getVer(){ return ver;}  //取得结点值(顶点集) 

    edgeNode<TypeOfEdge> *getHead(){ return head;}  //取得对应的边表的头指针 

    void setVer(TypeOfVer value){ ver = value;}  //设置结点值(顶点集) 

    void setHead(edgeNode<TypeOfEdge> *value){ head = value;}  //设置对应的边表的头指针

};

template <class TypeOfVer, class TypeOfEdge>

class adjlist_graph{

    private:

       int Vers;           //顶点数 

       int Edges;          //边数 

       verNode<TypeOfVer,TypeOfEdge> *verList;

       

       string GraphKind;     //图的种类标志 

       

       bool Delete_Edge( int u, int v ); 

       bool DFS(int u, int &num, int visited[]); //DFS遍历(递归部分)

    public:

       adjlist_graph( const string &kd, int vSize, const TypeOfVer d[]); //构造函数构造一个只有结点没有边的图。 

       adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e); 构造函数构造一个无权图。5个参数的含义:图的类型、结点数、边数、结点集和边集 

       adjlist_graph( const string &kd, int vSize, int eSize, const TypeOfVer d[], int **e, const TypeOfEdge w[]); //构造函数构造一个有权图。

       bool GraphisEmpty() { return Vers == 0; }  //判断图空否

       string GetGraphKind(){ return GraphKind; }

       bool GetVer(int u, TypeOfVer &data); //取得G中指定顶点的值 

       int GetFirstAdjVex(int u, int &v); //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1

       int GetNextAdjVex(int u, int v, int &w); //返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回false

       bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值 

       bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点 

       int LocateVer(TypeOfVer data); //返回G中指定顶点的位置 

       bool ExistEdge(int u, int v);

       bool PrintVer();  //输出顶点集 

       bool PrintAdjList();  //输出邻接矩阵 

       int GetVerNum(){ return Vers;}    //取得当前顶点数 

       int GetEdgeNum(){ return Edges;}  //取得当前边数 

       bool Insert_Edge(int u, int v); //无权图插入一条边

       bool Insert_Edge(int u, int v, TypeOfEdge w); //有权图插入一条边

       bool DeleteVer(const TypeOfVer &data); //往G中删除一个顶点 

       bool DeleteEdge( int u, int v ); //删除边 (外壳:有向(删除1条边), 无向(删除2条边))

       void DFS_Traverse(int u); //DFS遍历(外壳部分)

       void BFS_Traverse(int u); //BFS遍历

       ~adjlist_graph(); //析构函数 

};

输入说明 :

第一行:图的类型

第二行:结点数

第三行:结点集

第四行:边数

第五行:边集

第六行:权集

输出说明 :

第一行:图的类型

第二行:顶点集

第三行:邻接表

输入范例 :

UDN
6
A B C D E F
6
0 1
0 2
1 3
2 3
3 4
3 5
20 30 40 50 70 80

--------------------------------

UDN
A B C D E F
A->2(30)->1(20)->nullptr
B->3(40)->0(20)->nullptr
C->3(50)->0(30)->nullptr
D->5(80)->4(70)->2(50)->1(40)->nullptr
E->3(70)->nullptr
F->3(80)->nullptr

这道题难度是易,但我觉得这道题很难,可能我是一个新手刚上手有点恶心。

我觉得这道题很恶心的地方在于她的权值输入的格式很恶心

UDN
6
A B C D E F
6
0 1
0 2
1 3
2 3
3 4
3 5
20 30 40 50 70 80

这种输入就很caodan

你如果这样输入的话 我的代码量可以少很多

UDN
6
A B C D E F
6
0 1 20
0 2 30
1 3 40
2 3 50
3 4 70 
3 5 80

他把权重和边分离输入了 所以很难受

下面附上我的代码  

我的代码逻辑很简单

首先分为 无向网和有向网

各自有两个核心函数

一个是构造邻接表 对DN来说就是 creatdirection

对UDN来说 就是creatnodirection

------------------------------------------

构造完成后 第二个核心函数

给邻接表附上权重

DN来说就是addweight

UDN就是addweight——no——direction

----------------------------

然后各自输出  

#include<iostream>
#include<queue>
using namespace std;
int m = 0;
int arr[100][2] = {0};
struct bian
{
	int num;
	int weight;
	bian* next=NULL;
};

struct point
{
	string letter;
	bian* next=NULL;
};

struct graph
{

	point m[100];
	int number_of_point;
	int number_of_bian;
};

void addweight_no_direction(graph &g);
void addweight(graph& g);


void creat_no_directioin(graph &g)
{
	int e;
	int f;
	cin >> e;
	g.number_of_point = e;
	for (int i = 0; i < e; i++)
	{
		cin >> g.m[i].letter;
	}
	//上面是输入的顶点的字母

	cin >> f;
	g.number_of_bian = f;
	for (int i = 0; i < f; i++)
	{
		int v1;
		int v2;
		cin >> v1 >> v2;
		arr[i][0] = v1;
		arr[i][1] = v2;
		bian* p1 = new bian;
		p1->num = v2;
		 p1->next= g.m[v1].next ;
		g.m[v1].next = p1;
		bian* p2 = new bian;
		p2->num = v1;
		 p2->next= g.m[v2].next ;
		g.m[v2].next = p2;
	}

	addweight_no_direction(g);

}
void addweight_no_direction(graph& g)
{
	for (int j = 0; j < g.number_of_bian; j++)
	{
		int temp = 0;
		cin >> temp;
		int number1 = arr[j][0];
		int number2 = arr[j][1];
		bian* p;
		bian* p1;
		p = g.m[number1].next;
		p1 = g.m[number2].next;
		while (p)
		{
			if (p->num == number2)
			{
				p->weight = temp;

			}
			p = p->next;
		}

		while (p1)
		{
			if (p1->num == number1)
			{
				p1->weight = temp;
			}
			p1 = p1->next;
		}
	}
}


void creat_direction(graph &g)
{
	int e;
	int f;
	cin >> e;
	g.number_of_point = e;
	for (int i = 0; i < e; i++)
	{
		cin >> g.m[i].letter;
	}
	//上面是输入的顶点的字母

	cin >> f;
	g.number_of_bian = f;
	for (int i = 0; i < f; i++)
	{
		int v1;
		int v2;
		cin >> v1 >> v2;
		arr[i][0] = v1;
		arr[i][1] = v2;
		bian* p1 = new bian;
		p1->num = v2;
		p1->next=g.m[v1].next;
		g.m[v1].next = p1;
	}


	addweight(g);

}
void addweight(graph& g)
{
	for (int j = 0; j < g.number_of_bian; j++)
	{
		int temp = 0;
		cin >> temp;
		int number1 = arr[j][0];
		int number2 = arr[j][1];
		bian* p;

		p = g.m[number1].next;
		
		while (p)
		{
			if (p->num == number2)
			{
				p->weight = temp;

			}
			p = p->next;
		}		
	}
}




int main()
{

	string kind;
	cin >> kind;
	if (kind == "DN")//
	{
		graph g;
		int m = 0;
		creat_direction(g);





		cout << "DN" << endl;
		for (int h = 0; h < g.number_of_point; h++)
		{
			if (m == 1)
			{
				cout << " ";
			}
			m = 1;
			cout << g.m[h].letter;
		
		}
		cout << endl;
		for (int u = 0; u < g.number_of_point; u++)
		{
			cout << g.m[u].letter;
			
			
			bian* p = g.m[u].next;
			while (p)
			{
				cout << "->";
				cout << p->num << "(" << p->weight << ")";
				p = p->next;
			}
			if (p == NULL)
			{
				cout << "->" << "nullptr" << endl;
			}
		}
	}

	else if (kind == "UDN")//
	{
	
		graph g;
		creat_no_directioin(g);




		cout << "UDN" << endl;
		for (int h = 0; h < g.number_of_point; h++)
		{
			if (m == 1)
			{
				cout << " ";
			}
			m = 1;
			cout << g.m[h].letter;

		}
		cout << endl;
		for (int u = 0; u < g.number_of_point; u++)
		{
			cout << g.m[u].letter;


			bian* p = g.m[u].next;
			while (p)
			{
				cout << "->";
				cout << p->num << "(" << p->weight << ")";
				p = p->next;
			}
			if (p == NULL)
			{
				cout << "->" << "nullptr" << endl;
			}
		}
	}

	

	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,让我来为你讲解如何构造无权邻接表邻接表是一种的存储结构,通过记录每个顶点的邻接点来表示。对于无权,我们可以通过记录每个顶点的邻接点来构造邻接表。 具体的构造过程如下: 1. 创建一个数组,数组的长度为中顶点的个数。 2. 对于每个顶点,创建一个链表,链表中存储该顶点的邻接点。 3. 遍历中的每条边,将每个顶点的邻接点加入到对应的链表中。 举个例子,假设我们有一个无向,其中有 4 个顶点和 4 条边,边的连接关系如下: ``` 1 -- 2 | | 3 -- 4 ``` 我们可以按照上述步骤构造邻接表: 1. 创建一个长度为 4 的数组。 2. 对于每个顶点,创建一个链表: ``` v0: -> NULL v1: -> NULL v2: -> NULL v3: -> NULL ``` 3. 遍历每条边,将每个顶点的邻接点加入到对应的链表中: - 边 (1, 2):将 2 添加到 v1 的链表中,将 1 添加到 v2 的链表中。 ``` v0: -> NULL v1: -> 2 -> NULL v2: -> 1 -> NULL v3: -> NULL ``` - 边 (1, 3):将 3 添加到 v1 的链表中,将 1 添加到 v3 的链表中。 ``` v0: -> NULL v1: -> 2 -> 3 -> NULL v2: -> 1 -> NULL v3: -> 1 -> NULL ``` - 边 (2, 4):将 4 添加到 v2 的链表中,将 2 添加到 v4 的链表中。 ``` v0: -> NULL v1: -> 2 -> 3 -> NULL v2: -> 1 -> 4 -> NULL v3: -> 1 -> NULL v4: -> 2 -> NULL ``` - 边 (3, 4):将 4 添加到 v3 的链表中,将 3 添加到 v4 的链表中。 ``` v0: -> NULL v1: -> 2 -> 3 -> NULL v2: -> 1 -> 4 -> NULL v3: -> 1 -> 4 -> NULL v4: -> 2 -> 3 -> NULL ``` 最终得到的邻接表如上所示。 希望这个解答能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨骅麟(Patrick Young)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值