0

I'm new in coding, and this is my first attempt on graphs. Basically I have to find the shortest path between two locations using graphs. The arrival time and departure time will be given to us. I am having problem in creating the graph. In the void addloc function I want to add the location to the array of adjacency list everytime I add a new location. How do I do that? Also it would be really helpful if someone could tell me how I can keep the cities as string instead of int. If i keep them as strings I can't use the list as it takes only int or enum as input.

` using namespace std;

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);
}`

1 Answer 1

0

In the function void addloc(int a,graph *flight), you are initializing the graph itself.

flight = new graph;

You should initialize the adjacency list for the new city. See here: C++ lists and pointers

You can use map for storing city names in string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.