C++, Nested Classes, Cross Referencing

  adjacency-list, c++, cross-reference, data-structures

Last week, I was working on a homework, that asked us to implement a directed graph data structure. I decided to implement my graph as an adjacency list. below in the picture, you can see the idea I had.İmage 1
To implement this structure, I created two nested structures. CityListNode, and RouteListNode. (In the homework, we were building a software for an airline company) In this implementation, vertexes are implemented as a linked list, and each edge between vertexes is also a linked list. When I tried to use a pointer in each edge to point back to the vertex the edge was connecting, I started facing errors due to cross-referencing. Below you can see a simplified version of the class declaration. Is there a way to fix this error? Should I always stay away from cross-referencing? How would you implement two different nodes having pointers to each other?
Thanks in advance 🙂

class AirlineGraph{
private:
    class routeListNode{
        public:
            cityListNode* destination;
            routeListNode *next;   
    };
    class cityListNode{
        public:
            string cityName;
            int cityNumber;
            cityListNode *next;
            routeListNode *root;   
    };
    int numCity , numRoute;
    cityListNode* cityListRoot;
    ...
    ...
    ...
 };

Source: Windows Questions C++

LEAVE A COMMENT