Help with implementing adjacency list

Hello guys I need help with creating the framework behind a current project. I am not going to ask you to do it for me, I just need some guidance.

My task is to implement an adjacency list in C++, which will consist of nodes that will store a string (the name of the city) as well as an integer to represent the "total distance in meters" between 2 nodes (cities). I will be given a document consisting of the different pathways between nodes and the number of meters each node is seperated by. At the bottom is a link to just a picture of what I'm trying to make.

I am trying to make a class called DLL which will contain a struct called "node" to hold the needed values.

My code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

class DLL {
private:  
    struct Node {
        string nameOfCity;
        Node *next;
        //Node *prev;
        int distance;
    };
    Node *head = nullptr;  //last element
    Node *tail = nullptr;    //1st element
public:
    DLL() {    //new left-hand side node
        Node *head = nullptr;   
    }
    DLL(string pCityName, int pCost)
    {
        Node *n = new Node;
        n->nameOfCity = pCityName;
        n->distance = pCost;
        n->next = nullptr;
    }
    ~DLL()
    {
        while(head != nullptr)
        {
            Node *n = head->next;
            delete head;
            head = n;
        }
    }
    void addNode(string pName)  
    {
        Node *n = new Node;
        n->nameOfCity = pName;
        //n->prev = nullptr;  
        n->next = head;
        head = n;
    }

};


Am I on the right track?

Also, another question: I want the left-hand side list to be a doubly linked list, but the nodes on the right side singly linked. How do I accomplish this?

Thank you guys so much!

link:http://postimg.org/image/5ohpw1rv9/
Last edited on
Topic archived. No new replies allowed.