C 图数据结构
C++ Graph data Structure
我在C 中编写了一个图形实现,其中城市是顶点,从一个城市到另一个城市的航班代表一个边缘,权重是这些城市之间的距离。顶点,边缘和权重存储在文件中,当程序运行时,将顶点,边缘和权重加载到程序中。我正在使用一个代表边缘的邻接矩阵。
现在,当程序运行时,它将提示用户:
- 选择出发城
- 退出。选项两个刚刚终止了程序。
如果用户选择选项ONE,则将列出文件中的所有城市。我选择了一个七个城市。因此看起来像1.)洛杉矶2.)纽约3.)迈阿密等直到选项7。当用户选择选项时,它将列出除用户选择的出发城市以外的所有目标城市。一旦用户选择他的目的地城市,将会有三种可能性。
现在,第一个可能性将没有直接的,或者通过城市A和城市B之间的联系,并且该计划将输出,[出发城市]和[目的地城市]之间没有目的地,请按任何钥匙返回。用户按任何键,菜单将再次显示。第二种可能性是,如果城市之间存在直接联系,那么该计划将输出[出发城市] - [目的地城市] = [Miles]与城市之间的里程之间的直接连接,或者没有直接连接将说没有直接连接,用户可以返回菜单。
第三种可能性是将有一个连接,它将显示出离开的城市和目的地城市,它们之间的所有城市以及它们之间的总里程,用户可以按任何键以返回到菜单。
现在,问题i" M的问题是从文件中获取信息,我无法弄清楚如何从文件中获取信息或如何编写文件,因此程序知道哪些是顶点,边缘和权重。此外,如何展示城市并通过连接或根本没有连接具有直接连接的城市。
include <iostream>
#include <fstream>
#pragma once
const int NULL_EDGE = 0;
typedef std::string String;
class GraphType
{
private:
int edges[50][50];
int m_numVertices;
int m_maxVertices;
int m_distance;
String* m_vertices;
bool* marks; // marks[i] is the mark for vertices[i]
int IndexIs(String*, String);
public:
GraphType();
~GraphType();
bool isEmpty() const;
bool isFull(); //to do
int GetWeight(String, String); // to do
void ClearMarks(); // to do
void MarkVertex(String) // to do
bool isMarked(String) // to do
void addVertex(String);
void addEdge(String, String, int);
void displayCities();
};
#include "GraphType.h"
GraphType::GraphType()
{
m_maxVertices = 50;
m_distance = 0;
m_vertices = new String[m_maxVertices];
marks = new bool[50];
std::ifstream loadFile;
loadFile.open("load.txt");
if (loadFile.fail())
std::cout << " Error opening load.txtn";
else
{
//stuck here
}
loadFile.close();
}
GraphType::~GraphType()
{
delete[] m_vertices;
delete[] marks;
}
int GraphType::IndexIs(String* vertices, String vertex)
{
int index = 0;
while (!(vertex == m_vertices[index]) == 0)
index++;
return index;
}
void GraphType::addVertex(String vertex)
{
m_vertices[m_numVertices] = vertex;
for (int i = 0; i < m_numVertices; i++)
{
edges[m_numVertices][i] = NULL_EDGE;
edges[i][m_numVertices] = NULL_EDGE;
}
m_numVertices++;
}
void GraphType::addEdge(String startVertex, String destVertex, int weight)
{
int row;
int col;
row = IndexIs(m_vertices, startVertex);
col = IndexIs(m_vertices, destVertex);
edges[row][col] = weight;
}
void GraphType::displayCities()
{
//stuck here
}
bool GraphType::isEmpty() const
{
return (m_numVertices == 0);
}
#include "GraphType.h"
int FlyMenu();
void CitiesMenu(GraphType&);
int main()
{
int choose;
GraphType gt;
do
{
choose = FlyMenu();
switch (choose)
{
case 1: CitiesMenu(gt);
break;
case 2:
break;
default: std::cout << " Invalid Inputn";
break;
}
} while (choose != 2);
return 0;
}
int FlyMenu()
{
int option;
std::cout << " 1.) Choose Depature Cityn";
std::cout << " 2.) Exitn";
std::cout << " Enter option: ";
std::cin >> option;
return option;
}
void CitiesMenu(GraphType& gt)
{
gt.displayCities();
}
我知道深度遍历和宽度遍历算法,以查看城市之间是否存在联系,但我不知道如何在这种情况下实施它们。我不能使用标准模板库,只有std :: vector。我正在考虑写另一堂课,但我不知道那堂课会对我有什么帮助。
从我得到的东西中有两个问题:
- 从文件中读取/写入:最简单的解决方案将是频率的。
freopen("input","r",stdin)
freopen("output","w",stdout)
现在,每个cin & cout
操作都将在您在freopen
- 实施DFS/BFS:我将为您编码最简单的DFS类型,并且您必须对其进行编辑以进行编码。
bool visit[MAX_CITY + 10];
void DFS(int x){
visit[x] = 1;
for (int i=1;i<=MAX_CITY;i++){
int nx = i;
if (adj[x][i] == -1) continue; // a cost of -1 means no flight
if (visit[nx]) continue;
DFS(nx);
}
}
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 带参数的数据结构的全局声明