You are not logged in. Please login at www.codechef.com to post your questions!

×

can't undertand double pointer to structure

Hello so I'm trying to implement graph data structure in c++ with adjacency list. I am using structures for that.

struct node{
int src;
node*next;
};

struct Graph{
int vertex;
node**adj;  //for storing head pointers of adjacency list
};

Now here how do I allocate memory dynamically for storing V(vertex) no. of head pointers of adjacency lists in adj(of graph structure). I tried something like

graph*g=new graph();
*(g->adj)=new node*[v]; //v is no of vertices

But I think its wrong. And also how to access this adj(of graph)? I tried like this for adding an edge between two vertex.

temp->next=*(g->adj[src]) //here temp is newly created node pointer and g is a graph pointer.
*(g->adj[src])=temp;      //src is the source vertex

I think the syntax is incorrect. Please help.

asked 27 Jul '17, 15:59

amartya_k's gravatar image

1★amartya_k
22
accept rate: 0%


This one is the correct approach:

graph* g = new graph();
g->adj = new node*[v];
for (int src = 0; src < v; ++src) {
    node *head = g->adj[src] = new node(); // allocate the head
    head->src = src;
}

since new node*[v] is of type node**, which is also the type of g->adj. But that syntax merely allocates an array of pointers, so you have to properly allocate each head as well. Pointers are unallocated by default, after all. Hope that is clear.

link

answered 27 Jul '17, 18:46

hikarico's gravatar image

5★hikarico
1.9k1515
accept rate: 28%

edited 27 Jul '17, 18:49

2

I was about to say the same thing. Here is a small working demo: link.
Although, @amartya_k, usually working with library containers is the better choice compared to implementing your own linked list.

(27 Jul '17, 18:52) meooow ♦6★

Thanks everyone. This really helped me alot. I also asked this question on stack overflow but none of the guys could understand it. I was really surprised to see that and they flagged my question. @meooow I know the vector implementation but I just wanted to try the native way first because I believe that's good for understanding when you are a beginner. :)

(31 Jul '17, 01:21) amartya_k1★

@amartya_k, agreed :)

(31 Jul '17, 18:33) meooow ♦6★

Look if you want to implement adjacency list you can use array of pointers instead of **adj. This is because when you implement a graph by adjacency list you have to make head nodes and then pointer in the strucutre of nodes should point to its adjacent elements. Agree? In that case there will be two structures one for nodes made overall and another for those nodes which are adjacent. For eg:

1 -4-3
2 -1-3
3 -4-2
4 -2-1

This is a random representation. So for first column, you need a different structure and for rest you need different. So instead of having all this why not make an array of pointers for first column. In this way you have to make only one structure. There are different methods for implementing it. It might be possible that you didn't get this one but atleast you can try.This might help you---> http://www.geeksforgeeks.org/graph-and-its-representations/ Hope it Helps :)

EDIT: In case you think why i said that there will be two structures because since you have taken **adj so you have to manually link nodes 1 2 3 4 in this example and also these nodes are pointing to adjacent nodes so for this structure there are two pointers and for those adjacent nodes there is a single pointer as you can see 4-3,1-3,4-2...

link

answered 27 Jul '17, 17:58

vishesh_345's gravatar image

1★vishesh_345
2567
accept rate: 8%

edited 27 Jul '17, 18:18

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,916
×1,409
×1,236
×98
×62

question asked: 27 Jul '17, 15:59

question was seen: 1,018 times

last updated: 31 Jul '17, 18:33