This program is supposed to show the adjacency lists that are built given the input pairs::

0-2, 1-4, 2-5, 3-6, 0-4, 6-0, and 1-3

I am having trouble with the syntax of my code. Could someone please help?

#include <iostream>

using namespace::std;

int V;

struct node
  { int v; node* next;
    node(int x, node* t)
    {v=x; next = t;}
typedef node *link;
int main()
  { int i, j; link adj[V];
    for (i=0; i<V; i++) adj[i] = 0;
     while (cin >> i >> j)
       adj[j] = new node(i, adj[j]);
       adj[i] = new node(j, adj[i]);


typedef node *link;

You can't use typedefs like that in modern C++. When you declare adj , it's probably better to simply use node * .

Also, when you declare adj, you're using V as the array size, which you never initalized the variable, so you it would contain random junk. To remedy the problem, I suggest using a const int for V like this:

const int V = 5; // (for example)

Hope this helps

commented: correct and thanks +1

given the example in this article and and the example you posted, where "->" represents a linked list, you would have a 2d linked list that looks like below ?


The first number in the pair is the row number, and the second number is a node in the list represented by the row number.

Is that how you define your problem?

That defines the problem to a "T". Thanks Ancient Dragon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.