The abstract data type about graph

timestamp : 2020.12.31 status: unfinished

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>

using namespace std;

typedef int vertextype;
typedef int weight;
typedef int index;

struct vNode
{
	struct arcNode* next;
	index id;
	vertextype ele;
};

struct arcNode
{
	struct arcNode* next;
	index id;
};

struct edge
{
	vertextype v1;
	vertextype v2;   
	edge(vertextype v1, vertextype v2) : v1(v1), v2(v2) { }
};

class adjGraph
{

public:
	//void insertEdge(edge);
	void initEdge();
	void initEdge(string path);
	void initVertex();
	void initAdjGraph();
	void printAdjGraph();
public:
	vector<vNode*> adjList;
	vector<vertextype> vertexVec;
	vector<edge> edgeVec;
	map<vertextype, index> _value2index;
	map<index, vertextype> _index2value;
	int _vexNum;
	int _arcNum;
};


void adjGraph::initVertex()
{
	vertexVec = { 1,2,3,4,5 };
	_vexNum = vertexVec.size();
}

void adjGraph::initEdge()
{
	edgeVec.push_back(edge(1, 2));
	edgeVec.push_back(edge(1, 3));
	edgeVec.push_back(edge(1, 4));
	edgeVec.push_back(edge(2, 1));
	edgeVec.push_back(edge(2, 3));
	edgeVec.push_back(edge(2, 4));
	edgeVec.push_back(edge(2, 5));
	edgeVec.push_back(edge(3, 1));
	edgeVec.push_back(edge(3, 2));
	edgeVec.push_back(edge(3, 4));
	edgeVec.push_back(edge(4, 1));
	edgeVec.push_back(edge(4, 2));
	edgeVec.push_back(edge(4, 3));
	edgeVec.push_back(edge(4, 5));
	edgeVec.push_back(edge(5, 2));
	edgeVec.push_back(edge(5, 4));
	_arcNum = edgeVec.size();
}

void adjGraph::initEdge(string path)
{
	ifstream ifs;
	ifs.open(path.c_str(), ios::in);
	if (!ifs.is_open()) cout << "error for open file" << endl;
	string line;
	while (getline(ifs, line))
	{
		stringstream ss(line);
		int value1;
		ss >> value1;
		int value2;
		ss >> value2;
		edgeVec.push_back(edge(value1, value2));
	}
	_arcNum = edgeVec.size();
}

void adjGraph::initAdjGraph()
{
	for (int i = 0; i < _vexNum; i++)
	{
		vNode* ptrVNode = new vNode;
		ptrVNode->id = i;
		ptrVNode->ele = vertexVec.at(i);
		ptrVNode->next = nullptr;
		adjList.push_back(ptrVNode);
		_value2index.insert(pair<vertextype, index>(ptrVNode->ele, ptrVNode->id));
		_index2value.insert(pair<index, vertextype>(ptrVNode->id, ptrVNode->ele));
	}

	for (int i = 0; i < _arcNum; i++)
	{
		index idx1 = _value2index[edgeVec.at(i).v1];
		index idx2 = _value2index[edgeVec.at(i).v2];
		arcNode* ptrArcNode = new arcNode;
		ptrArcNode->id = idx2;
		ptrArcNode->next = nullptr;
		
		if (adjList.at(idx1)->next == nullptr)
		{
			adjList.at(idx1)->next = ptrArcNode;
		}
		else
		{
			arcNode* tmpNode = adjList.at(idx1)->next;
			while (tmpNode->next != nullptr)
			{
				tmpNode = tmpNode->next;
			}
			tmpNode->next = ptrArcNode;
		}
	}
}

void adjGraph::printAdjGraph()
{
	for (int i = 0; i < _vexNum; i++)
	{
		cout << adjList.at(i)->ele << " ------> ";
		arcNode* tmpNode = adjList.at(i)->next;
		while (tmpNode)
		{
			cout << _index2value.at(tmpNode->id) << " ------> ";
			tmpNode = tmpNode->next;
		}
		cout << endl;
	}
}

void test()
{
	adjGraph adj;
	adj.initVertex();
	adj.initEdge("file.txt");
	adj.initAdjGraph();
	adj.printAdjGraph();
}

int main()
{
	test();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值