jeudi 27 février 2020

C++ error: cannot convert ‘Graph

I am trying to make construct a graph using adjacency list. I have used template for the same. However, I am facing an error

I am assigning one variable (v_node *) to another of the same type. But this leads to an error. This might be happening to to the template < class T > that I used.

Please Suggest a solution.

Here is the code :

template < class T >
class Graph
{
    public:
        Graph()
        {
            h = NULL;
            H = NULL;
        }
        virtual ~Graph()
        {

        }
        void insertEdge(T, T)
        {
            struct v_node *a, *b, *c;
            struct e_node *tmp;
            c = H;
            if(c == NULL)
                return;
            while(c != NULL && c -> data != data1)
            {
                if(c == NULL)
                    break;
                c = c -> nxt;
            }
            if(c == NULL)
                return;
            a = c;
            c = H;
            while(c != NULL && c -> data != data2)
            {
                if(c == NULL)
                    return;
                c = c -> nxt;
            }
            if(c == NULL)
                return;
            b = c;
            if(a -> enode == NULL)
            {
                 a -> enode = new e_node;
                 tmp = a -> enode;
                 tmp -> vnode = b;
                 tmp -> nxt = NULL;
            }
            else
            {
                struct e_node *tmp = a -> enode;
                while(tmp -> nxt != NULL)
                {
                    tmp = tmp -> nxt;
                }
                tmp -> nxt = new e_node;
                tmp -> nxt -> nxt = NULL;
                tmp -> nxt -> vnode = b;
            }
            if(b -> enode == NULL)
            {
                 b -> enode = new e_node;
                 b -> enode -> vnode = a;
                 b -> enode -> nxt = NULL;
            }
            else
            {
                struct e_node *tmp = b -> enode;
                while(tmp -> nxt != NULL)
                {
                    tmp = tmp -> nxt;
                }
                tmp -> nxt = new e_node;
                tmp -> nxt -> nxt = NULL;
                tmp -> nxt -> vnode = a;
            }
        }
        void add(T)
        {
            if(H == NULL)
            {
                H = new v_node;
                H -> nxt = NULL;
                H -> enode = NULL;
                H -> data = data;
                return;
            }
            v_node *tmp = H;
            while(tmp -> nxt != NULL)
            {
                tmp = tmp -> nxt;
            }
            tmp -> nxt = new v_node;
            tmp -> nxt -> nxt = NULL;
            tmp -> nxt -> enode = NULL;
            tmp -> nxt -> data = data;
            return; 
        }
        void display()
        {
            struct v_node *p = H;
            struct e_node *q = NULL;
            while(p != NULL)
            {
                cout << p -> data << " : ";
                q = p -> enode;
                while(q != NULL)
                {
                    cout << q ->
                }
            }
        }
    private:
        struct e_node
        {
            struct v_node *vnode;
            struct e_node *nxt;
        }*h;
        struct v_node
        {
            struct v_node *nxt;
            struct e_node *enode;
            T data;
        }*H;
};

Aucun commentaire:

Enregistrer un commentaire