-3

I am trying to represent a graph using the adjacency list representation of graphs in C++. So I tried converting this C(language) code which I found in this youtube video https://www.youtube.com/watch?v=mQEv1QxaIuM&t=7s. But when I tried writing this code in C++, I'm getting an error. VSStudio Compiler showing "expression must have pointer type". (Please note that this video is in Hindi language.)

Here is my code in C++ (It's not yet completed because of the error.)

// Adjacency List Representation of Graph

#include <iostream>

struct listNode // defining linked list
{
    int vertexNumber;
    listNode *next;
};

struct graph // defining graph
{
    int vertex;
    int edge;
    listNode *adj;

};

graph *adjacencyListOfGraph()
{
    int x, y;
    listNode *temp;
    graph *G = new graph;

    std::cout << "Enter the number of vertex and edges.\n";
    std::cin >> G->vertex >> G->edge;

    G->adj = new listNode[G->vertex];

    for (int i = 0; i < G->vertex; ++i)
    {
        G->adj[i]->vertexNumber = i;// this is where I'm getting an error
    }
}
  • _So how can I convert this code in C++ without using any classes,vectors or malloc ,almost similar to this C code in pure C++ style_ - that defeats the purpose of using C++. – Maxim Egorushkin Sep 18 '18 at 16:38
  • @maxim Hmm, can't disagree with you though! – dr_invincible Sep 18 '18 at 16:55
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. Concerning your question, please extract a [mcve]. The fault will probably become quite obvious once you reduced your code to the smalles possible example showing that error. – Ulrich Eckhardt Sep 18 '18 at 17:15
  • @UlrichEckhardt Is it more straightforward now? If it is , then please don't downvote! – dr_invincible Sep 18 '18 at 17:26
  • You removed the (irrelevant) C code, which is good. However, why should I have to input anything? If the input isn't necessary, replace it with hardcoded values. If the loop isn't necessary, do the same. Keep doing that until there is nothing left to remove! – Ulrich Eckhardt Sep 18 '18 at 18:07
  • @UlrichEckhardt Enlighten me ,if you know a better way to represent a graph using adjacency list! – dr_invincible Sep 19 '18 at 01:42
  • That's not the point! You have a very specific problem with C++ itself, you just confused a pointer and a reference. This is completely separate from your goals with graphs. In order to understand a problem, you isolate it from the context you encountered the problem in, so that you can focus on just the problem without being distracted from the rest. This is a fundamental technique for problem solving (for solving /any/ problem!) and that's what the MCVE requirement forces you to practice. BTW: A better way would be using standard containers or a library. – Ulrich Eckhardt Sep 19 '18 at 06:02

1 Answers1

0

See the answer to this question. adj[i] is equivalent to *(adj+i). You are taking an offset from G->adj and then dereferencing, so G->adj[i].vertexNumber is the correct syntax.

(Note the correct spelling is adjacency too).

ChrisD
  • 927
  • 4
  • 10