【问题标题】:Adding a vertex to a graph using adjacency list使用邻接表向图中添加顶点
【发布时间】:2013-11-17 06:47:44
【问题描述】:

我是编码新手,这是我第一次尝试图表。基本上我必须使用图表找到两个位置之间的最短路径。到达时间和出发时间将提供给我们。我在创建图表时遇到问题。在 void addloc 函数中,我想在每次添加新位置时将该位置添加到邻接列表数组中。我怎么做?如果有人能告诉我如何将城市保持为字符串而不是整数,那将非常有帮助。如果我将它们保留为字符串,我将无法使用该列表,因为它只需要 int 或 enum 作为输入。

` 使用命名空间标准;

class location
{
    public:
        int city;
};

class flightconnect
{
        int arvtime;
        int deptime;
        int arvcity;
    public:
        flightconnect(int _a, int _d, int _c)  { arvtime = _a;  deptime = _d;  arvcity = _c;}
        int getdeptime()        {  return deptime; }
        int getarvtime()        {  return arvtime; }
        int getarvcity()        {  return arvcity; }
};

struct graph
{
    location* vertex;
    list<flightconnect> *adj;
};

void addloc(int a,graph *flight)
{
    flight = new graph;
    flight->vertex->city=a;
    //what should come in the next line?
    flight->adj = flight->adj.append(a);
}

void addflight(int at,int dt,int a,int d,graph *flight)
{
    flightconnect node(at,dt,d);
    flight->adj[a].push_back(node);
}`

【问题讨论】:

    标签: c++ data-structures graph adjacency-list


    【解决方案1】:

    在函数void addloc(int a,graph *flight) 中,您正在初始化图形本身。

    flight = new graph;
    

    您应该为新城市初始化邻接列表。见这里:C++ lists and pointers

    您可以使用地图将城市名称存储在字符串中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多