Graph: Adjacency list implementation through Linked list.

Hey guys, i have a problem in implementing adjacency list through linked list.
my vertex struct has an object of neighbor struct, but an object of vertex cant access neighbor struct.
Below is just a piece of code for the particular part of adjacency list where i am facing problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

using namespace std;

struct neighbor
{
    int data;
    int cost;
    neighbor *next;
};

struct vertex
{
    int data;
    vertex *next;
    neighbor *ed;
}*node;

int main()
{       
    node->ed->next=NULL;        // this gives me RUNTIME ERROR.
    
    return 0;
}
Last edited on
Anyone?
You never make node point to anything, nor ed.
node->ed isn't pointing to a valid object.
node does not point to anything. You first need to do node = new vertex; then node->ed = new neighbor; and only then you can work with node->ed->next. See a tutorial on dynamic memory.
wow, a triple post. that's rare :)
Last edited on
Thanks guys... and thanks hamsterman that worked. :)
Topic archived. No new replies allowed.