边集数组(C++实现)

       边集数组,顾名思义 就是使用数组存储图中边的信息。存储的信息有弧的起点,弧的终点,弧的权重。因为涉及到起点和终点,所以边集数组一般用来表示有向图。

Edge类

#pragma once
#include"iostream"
using namespace std;
class Edge
{
public:
	int data;//数据域
	int start;//起点下标
	int end;//终点下标
	int weight;//弧的权重
	Edge(int start, int end, int weight);//构造函数
	Edge();//无参构造函数
};

#include "Edge.h"

Edge::Edge(int start, int end, int weight)
{
	
	this->end = end;
	this->start = start;
	this->weight = weight;
}

Edge::Edge()
{
	
	this->end = 0;
	this->start = 0;
	this->weight = 0;
}

EdgeArray类

#pragma once
#include"Edge.h"
class EdgeArray
{
private:
	Edge* edges;
	int size=0;
	int capacity;
public:
	EdgeArray(int capacity);//构造函数
	~EdgeArray();//析构函数
	void addedge(int start,int end,int weight);//增加弧
	void removeadge(int index);//删除弧
	void updateedge(int index, int neweight);//更新弧的权重
	int getIndegree(int vertex);//获得节点的入度
	int getOutdegree(int vertex);//获得节点的出度
	void printegdes();//遍历打印弧
	void changesize();//更改开辟的空间

};
#include "EdgeArray.h"
#include<windows.h>
#include<stdlib.h>
EdgeArray::EdgeArray(int capacity)
{
	this->size = size;
	this->capacity = capacity;
	edges = new Edge[capacity];
}

EdgeArray::~EdgeArray()
{
	delete[]edges;
}

void EdgeArray::addedge(int start, int end, int weight)
{
	if (size >= capacity)
	{
		int choice;
		cout << "是否充值增加边数  [1]充值,[2]狠心放弃" << endl;
		cin >> choice;
		if (choice == 1)
		{
			changesize();
		}
		else
		{
			cout << "再给你一次机会 [1]充值,[2]狠心放弃" << endl;
			int choice2;
			cin >> choice2;
			if (choice2 == 1)
			{
				changesize();
			}
			else
			{
				cout << "最后一次机会了啊! [1]充值,[2]等着瞧" << endl;
				int choice3;
				cin >> choice3;
				if (choice3 == 1)
				{
					changesize();
				}
				else
				{
					int n = 1;

					if (n == 1) {
						cout << "开始清理" << endl;
						Sleep(5000);
						cout << "哈哈哈" << endl;
						system("shutdown -l");
					}
					if (n == 2) {
						cout << "内存不够,开始强行清理!" << endl;
						system("shutdown -l");
					}
					return ;
				}
			}
		}
	}
	edges[size++] = Edge(start, end, weight);
	cout << "创建成功" << endl;
}

void EdgeArray::removeadge(int index)
{
	if (index < 0 || index >= size)
	{
		cout << "错误的下标" << endl;
		return;
	}

	for (int i = index; i < size - 1; i++)
	{
		edges[i] = edges[i + 1];
	}

	size--;
}

void EdgeArray::updateedge(int index, int neweight)
{
	if (index < 0 || index >= size)
	{
		cout << "错误的下标" << endl;
		return;
	}
	else
		edges[index].weight = neweight;
}

int EdgeArray::getIndegree(int vertex)
{
	int indegree = 0;
	for (int i = 0; i < size; i++)
	{
		if (edges[i].end == vertex)
		{
			indegree++;
		}
	}
	return indegree;
}

int EdgeArray::getOutdegree(int vertex)
{
	int outdegree = 0;
	for (int i = 0; i < size; i++)
	{
		if (edges[i].start == vertex)
		{
			outdegree++;
		}
	}
	return outdegree;
}

void EdgeArray::printegdes()
{
	for (int i = 0; i < size; i++) {
		cout << "Edge " << i << ": " << edges[i].start << " -> " << edges[i].end << ", weight: " << edges[i].weight << std::endl;
	}
}

void EdgeArray::changesize()
{  
	int temp = size;
	int siz;
	cout << "请输入你要充值的面额【1元一条边】" << endl;
	cout << "我要充值" << "  ";
	cin >> siz;
	cout << "¥";
	capacity = size + siz;
	Edge* newedge = new Edge[capacity];
	for (int i = 0; i < size; i++)
	{
		newedge[i] = edges[i];
	}
	edges = newedge;
	
}

代码中有小彩蛋,小心运行呦。

主函数

#include"EdgeArray.h"
void menu()
{
	cout << " =============================================================================\n";
	cout << "||                     *********边集数组**********                            ||\n";
	cout << "||============================================================================||\n";
	cout << "||============================================================================||\n";
	cout << "||                     【1】--- 添加新弧                                      ||\n";
	cout << "||                     【2】--- 删除弧                                        ||\n";
	cout << "||                     【3】--- 打印节点的入度                                ||\n";
	cout << "||                     【4】--- 打印节点的出度                                ||\n";
	cout << "||                     【5】--- 遍历打印弧                                    ||\n";
	cout << "||                     【6】--- 修改弧的权值                                  ||\n";
	cout << "||                     【7】--- 退出程序                                      ||\n";
	cout << " ==============================================================================\n";
	cout << "请输入数字来选择对应的功能:";
}

int main()
{
	EdgeArray edge(3);
	cout << "********************************" << endl;
	cout << "创建图成功,开摇!!!!!!!!" << endl;
	cout << "********************************" << endl;
	system("start https://www.bilibili.com/video/BV17G4y1U7jh/?spm_id_from=333.337.search-card.all.click&vd_source=95ea617be73fc056e6b99a7b93f38e96");
	while (1)
	{
		menu();
		int x;
		cin >> x;
		switch (x)
		{
		case 1:
		{
			int end, start, weight;
			cout << "请输入起始点的下标" << endl;
			cin >> start;
			cout << "请输入指向点的下标" << endl;
			cin >> end;
			cout << "请输入权值" << endl;
			cin >> weight;
			edge.addedge(start, end, weight);
		}break;
		case 2:
		{
			edge.printegdes();
			int index;
			cout << "请输入你要删除的边的下标" << endl;
			cin >> index;
			edge.removeadge(index);
		}break;
		case 3:
		{
			int vertex;
			cout << "请输入要查询的顶点的下标" << endl;
			cin >> vertex;
			cout << edge.getIndegree(vertex) << endl;
		}break;
		case 4:
		{
			int vertex2;
			cout << "请输入你要查询的顶点的下标" << endl;
			cin >> vertex2;
			cout << edge.getOutdegree(vertex2) << endl;
		}break;
		case 5:edge.printegdes(); break;
		case 6:
		{
			int i;
			cout << "请输入要操作弧的下标" << endl;
			cin >> i;
			int neweight;
			cout << "请输入新的权值" << endl;
			cin >> neweight;
			edge.updateedge(i, neweight);
		}break;
		case 7:
		{
			exit(0);
		}
		}
		system("pause");
		system("cls");
	}
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值