Reputation: 47
#include<stdio.h>
#include<conio.h>
struct GNode{
struct gnode* nextnode;
struct gnode* arcptr;
int Visited;
}
typedef struct GNode* Grnd;
struct ArcNode{
struct arcnode* nextarc;
struct acrnode* ndptr;
}
typedef struct ArcNode* Arc;
Grnd getGraphNode(){
Grnd NewNode=(Grnd)malloc(sizeof(struct GNode));
NewNode->nextnode=NULL;
NewNode->arcptr=NULL;
return NewNode;
}
Arc getArcNode(){
Arc NewArc=(Arc)malloc(sizeof(struct ArcNode));
NewArc->nextarc=NULL;
NewArc->ndptr=NULL;
return NewArc;
}
void join(Grnd *GNode1,Grnd *GNode2){
Arc NewArc=getArcNode();
NewArc->ndptr=(*GNode2);
NewArc->nextarc=(*GNode1)->arcptr;
(*GNode1)->arcptr=NewArc;
}
Grnd addNode(Grnd *Graph){
Grnd NewNode=getGraphNode();
if((*Graph)==NULL){
(*Graph)=NewNode;
return NewNode;
}
while((*Graph)->nextnode!=NULL)
Graph=Graph->nextnode;
(*Graph)->nextnode=NewNode;
return NewNode;
}
Considering the above C code : I am worried that when I call the join
function to join two graph nodes, will it actually join them? because as soon as the program goes out of the scope of join
, NewArc
doesn't exist. So when I try to find all the adjacent Nodes to a given node after creating the graph,will I be able to? and Why?
Upvotes: 1
Views: 105
Reputation: 182619
because as soon as the program goes out of the scope of join , NewArc doesn't exist.
That's right! The pointer itself disappears, but the memory it points to is still there, allocated for you. So you are free to continue to refer to it via other pointers, until you free
it.
(*GNode1)->arcptr=NewNode;
I think you meant NewArc
instead of NewNode
?
Upvotes: 2