typedef struct vertex{
int num;
struct vertex *next;
} Vertex;
Vertex *adj[1];
void buildList(){
Vertex *v=NULL;
Vertex *t=NULL;
v = malloc(1*sizeof(*v));
v->num = 1;
adj[0] = v; //a NODE with value 1
t = v;
v = malloc(1*sizeof(*v));
v->num = 1;
t->next = v; // and ANOTHER NODE but it should be the SAME NODE with the above one
t = v;
//v = malloc(1*sizeof(*v));
//v->num = 1;
//t->next = adj[0]; // causes infinite loop...
//t = v;
}
预期的输出是一个值为 1 的节点,其自身在其邻接列表中,输出类似于 1 -> 1。
我的问题是看起来我有两个不同的节点。当我对其中一个进行更改时,另一个不会更改,就像另一个节点一样。
例如,在构建列表后,如果我更改节点的值,我应该得到类似 3 -> 3 的输出。但我得到 3 -> 1。节点上的更改不会影响另一个。当我尝试将 adj[0] 指向 t->next 但是我得到一个无限循环......