|
|
@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#define _stprintf
|
|
|
|
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include<iostream>
|
|
|
|
|
|
|
|
#include<fstream>
|
|
|
|
|
|
|
|
#include<graphics.h>
|
|
|
|
|
|
|
|
#include<string>
|
|
|
|
|
|
|
|
#define LEFTBORDER 30
|
|
|
|
|
|
|
|
#define TOPBORDER 50
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// def edge class
|
|
|
|
|
|
|
|
class Edge {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
int VerAdj; // num of point
|
|
|
|
|
|
|
|
int cost; // weight of edge
|
|
|
|
|
|
|
|
Edge* link;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Edge();
|
|
|
|
|
|
|
|
// initialization
|
|
|
|
|
|
|
|
Edge(int _veradj, int _cost, Edge* _link) : VerAdj(_veradj), cost(_cost), link(_link)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int& get_VerAdj();
|
|
|
|
|
|
|
|
int& get_cost();
|
|
|
|
|
|
|
|
Edge*& get_link();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Vertex {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
std::string VerName;
|
|
|
|
|
|
|
|
std::string Description;
|
|
|
|
|
|
|
|
Edge* adjacent; // to neighbor
|
|
|
|
|
|
|
|
int mark; // NO
|
|
|
|
|
|
|
|
int x, y; // position
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// initialization
|
|
|
|
|
|
|
|
Vertex();
|
|
|
|
|
|
|
|
std::string& get_VerName();
|
|
|
|
|
|
|
|
std::string& get_Description();
|
|
|
|
|
|
|
|
Edge*& get_adjacent();
|
|
|
|
|
|
|
|
int& get_mark();
|
|
|
|
|
|
|
|
int& get_x();
|
|
|
|
|
|
|
|
int& get_y();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Graph_List {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Vertex* head;
|
|
|
|
|
|
|
|
int graphsize; // summary of number of vertex
|
|
|
|
|
|
|
|
int MaxCost;
|
|
|
|
|
|
|
|
IMAGE* map;
|
|
|
|
|
|
|
|
int insert_edge_do(int v1, int v2, int weight) {
|
|
|
|
|
|
|
|
if (head[v1].get_adjacent() == NULL) {
|
|
|
|
|
|
|
|
head[v1].get_adjacent() = new Edge(v2, weight, NULL);
|
|
|
|
|
|
|
|
return 0; // 若邻接表为空,直接加入新边
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Edge* p = head[v1].get_adjacent();
|
|
|
|
|
|
|
|
Edge* pt = p;
|
|
|
|
|
|
|
|
while ((p != NULL) && (p->get_VerAdj() < v2)) {
|
|
|
|
|
|
|
|
pt = p;
|
|
|
|
|
|
|
|
p = p->get_link();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
|
|
|
// 若到邻接表末端仍比v2小
|
|
|
|
|
|
|
|
pt->get_link() = new Edge(v2, weight, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == head[v1].get_adjacent() && p->get_VerAdj() > v2) { // 邻接第一个就比v2大
|
|
|
|
|
|
|
|
Edge* q = new Edge(v2, weight, NULL);
|
|
|
|
|
|
|
|
q->get_link() = p;
|
|
|
|
|
|
|
|
head[v1].get_adjacent() = q;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->get_VerAdj() == v2) {
|
|
|
|
|
|
|
|
return -4; // 插入边已经存在,返回-4
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != head[v1].get_adjacent() && p->get_VerAdj() > v2) { // 正常在邻接表中
|
|
|
|
|
|
|
|
Edge* q = new Edge(v2, weight, NULL);
|
|
|
|
|
|
|
|
pt->get_link() = q;
|
|
|
|
|
|
|
|
q->get_link() = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int delete_edge_do(int v1, int v2) {
|
|
|
|
|
|
|
|
Edge* p = head[v1].get_adjacent();
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
|
|
|
return -1; // 返回-1表示所删除的边不存在
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->get_VerAdj() == v2) { // 邻接表第一个就是v2
|
|
|
|
|
|
|
|
head[v1].get_adjacent() = p->get_link();
|
|
|
|
|
|
|
|
delete p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Edge* pt = p;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
|
|
|
|
if (p->get_VerAdj() == v2) {
|
|
|
|
|
|
|
|
pt->get_link() = p->get_link();
|
|
|
|
|
|
|
|
delete p;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pt = p;
|
|
|
|
|
|
|
|
p = p->get_link();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == NULL) { // 到邻接表末端仍未找到v2,则边不存在
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int delete_Graph_List() { // 删除内容
|
|
|
|
|
|
|
|
for (int i = 0; i < graphsize; i++) {
|
|
|
|
|
|
|
|
Edge* p = head[i].get_adjacent();
|
|
|
|
|
|
|
|
Edge* q = p;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
|
|
|
|
p = p->get_link();
|
|
|
|
|
|
|
|
delete q;
|
|
|
|
|
|
|
|
q = p;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] head;
|
|
|
|
|
|
|
|
delete map;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
int& get_MaxCost();
|
|
|
|
|
|
|
|
Vertex* get_head();
|
|
|
|
|
|
|
|
IMAGE* get_map();
|
|
|
|
|
|
|
|
// 按代号查找在顶点表中的位置
|
|
|
|
|
|
|
|
int get_index(int _mark);
|
|
|
|
|
|
|
|
// 按名称查找在顶点表中的位置
|
|
|
|
|
|
|
|
int get_index(std::string name);
|
|
|
|
|
|
|
|
// 创建图
|
|
|
|
|
|
|
|
int graph_con(std::fstream& file, char* mapname);
|
|
|
|
|
|
|
|
// 构造函数,构造一个空图
|
|
|
|
|
|
|
|
Graph_List();
|
|
|
|
|
|
|
|
~Graph_List();
|
|
|
|
|
|
|
|
// 返回节点总数
|
|
|
|
|
|
|
|
int number_of_vertices()const;
|
|
|
|
|
|
|
|
// 返回边的数目,不计重复边
|
|
|
|
|
|
|
|
int number_of_edges()const;
|
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
|
|
|
|
int save_file(char* filename);
|
|
|
|
|
|
|
|
bool is_graph_empty();
|
|
|
|
|
|
|
|
int get_weight(const int& v1, const int& v2);
|
|
|
|
|
|
|
|
// 插入节点
|
|
|
|
|
|
|
|
void insert_vertex(std::string _name, std::string _des, int _mark, int _x, int _y);
|
|
|
|
|
|
|
|
// 插入边
|
|
|
|
|
|
|
|
int insert_edge(int v1, int v2, int weight);
|
|
|
|
|
|
|
|
// 删除边
|
|
|
|
|
|
|
|
int delete_edge(int v2, int v1);
|
|
|
|
|
|
|
|
// 删除节点
|
|
|
|
|
|
|
|
int delete_vertex(const int& v);
|
|
|
|
|
|
|
|
// 修改节点元素
|
|
|
|
|
|
|
|
int modify_vertex(int v, std::string _n, std::string _des, int& _m, int& _x, int& _y);
|
|
|
|
|
|
|
|
int dshortest_path(const int v, int*& path);
|
|
|
|
|
|
|
|
// 最长路径
|
|
|
|
|
|
|
|
int dlongest_path(const int v, int*& path);
|
|
|
|
|
|
|
|
};
|