#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;
}
考慮到上面的C代碼:我很擔心,當我調用join
函數來連接兩個圖形節點時,它會真正加入它們嗎?因爲只要程序超出join
的範圍,NewArc
就不存在。因此,當我在創建圖表後嘗試查找給定節點的所有相鄰節點時,我能夠嗎?爲什麼?指向c中結構的指針的範圍?
但'NewArc'只是一個指針類型,是嗎?指向的內存由'malloc()'分配,並且在釋放之前將繼續有效。 – cdhowie 2013-04-11 16:05:15