简体   繁体   中英

Adjacency List representation in C++, using class and pointers, head to lists not working properly

I am trying to implement adjacency lists using classes and pointers in C++. I have created a class of vertices, with an array of pointers pointing at each vertex. further in each vertex class, their is a linked list that should contain adjacency list of that vertex.

The following code runs, but does not give the correct adjacency list correctly. The head of the adjacency list changes, I don't know why. during passing pointers to different functions, this must be happening. The head gets assigned to the edge that is given to the function addToEdgeList() in some cases, but does not stay what is desired.

Here is my code. I can use C++ STL vectors to do this but this pointers issue is bugging me too much.

#include <iostream>
#include <string>
class vertex;
int vertexCount = 0;
struct edge {
        int vertexIndex = 0;            //takes the index of vertex of which the edge is.
        edge *next = NULL;              //the adjacency list of that index.
};
class vertex    {
    std::string name;                   //name of the vertex. But it can only be called using numbers(0, 1, 2,...)
    struct edge *head;          //the beginning of the edge it points to.
    public:
        vertex()    {
            name = "";
            head = new edge;
            head = NULL;
        }
        vertex(std::string abc) {
            name = abc;
            head = NULL;
        //  vertexCount += 1;
        }
        edge * getEdgeBeg() {
            return head;
        }
        void setName(std::string abc)   {
            name = abc;
            vertexCount += 1;
        }
        void addToEdgeList(edge * e)    {
            if (head != NULL)   {
                edge *ptr = new edge;
                ptr = this->head;
                std::cout << name << "-" << ptr->vertexIndex << "-" << e->vertexIndex << std::endl;
                while(ptr->next != NULL)    {
                    ptr = ptr->next;
                }
                ptr->next = e;

            }
            else    {
                head = e;
            }
            std::cout << head->vertexIndex << " " << name << " " << e->vertexIndex << std::endl;    
        }
        void printEdgeList()    {
            edge *ptr = new edge;
            ptr = head;
            while(ptr)  {
                std::cout << " ->" << ptr->vertexIndex;
                ptr = ptr->next;
            }
            delete ptr; 
        }
        void printName()    {
            std::cout << name;
        }
        ~vertex()   {
            delete head;
        }
}*graph[15];


void addEdge(int vertex1, int vertex2)  {
    edge *temp1 = new edge;
    edge *temp2 = new edge;
    temp1->vertexIndex = vertex2;
    temp1->next = NULL;
    graph[vertex1]->addToEdgeList(temp1);
    temp2->vertexIndex = vertex1;
    temp2->next = NULL;
    graph[vertex2]->addToEdgeList(temp2);
    delete temp1;
    delete temp2;
}
void printGraph()   {
    int i;
    for(i = 0; i < vertexCount; i++)    {
        graph[i]->printName();
        std::cout << " (" << i << ")";
        graph[i]->printEdgeList();
        std::cout << std::endl;
    }
}

int main()  {
    vertex *vptr[15];

    vptr[0] = new vertex;
    vptr[0]->setName("A");
    graph[0] = vptr[0];

    vptr[1] = new vertex;
    vptr[1]->setName("B");
    graph[1] = vptr[1];

    vptr[2] = new vertex;
    vptr[2]->setName("C");
    graph[2] = vptr[2];

    addEdge(0, 1);
    addEdge(0, 2);
    addEdge(1, 2);
    printGraph();
}

You have several errors on deallocating your pointers.
First, don't do edge *ptr = new edge; but edge *ptr = head; and after, remove your delete on *ptr .
In addEdge function, you add your edges pointers to your graph, so, as they are pointers, you should not delete them at the end of the function, otherwise your graph will point on NULL.
This is my output:

1 A 1
0 B 0
A-1-2
1 A 2
0 C 0
B-0-2
0 B 2
C-0-1
0 C 1
A (0) ->1 ->2
B (1) ->0 ->2
C (2) ->0 ->1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM