#include<stdio.h>
#include<string.h>
#include<malloc.h>
int mark[10060];
int zhan[10060];
int di,ding;
int k,m,bg,u,v;
struct node
{
int data;
struct node *firstarc;
};
struct tu
{
struct node dingdian[200];
int bian;
int dingdiangeshu;
};
void creat(struct tu * &G)
{
int i;
for(i=0;i<=k-1;i++)
{
G->dingdian[i].data=i;
G->dingdian[i].firstarc=NULL;
}
while(m--)
{
// printf("111\n");
scanf("%d%d",&u,&v);
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->data=u;
p->firstarc=NULL;
struct node *q;
q=&G->dingdian[v];
while(q->firstarc) q=q->firstarc;
p->firstarc=q->firstarc;
q->firstarc=p;
p=(struct node *)malloc(sizeof(struct node));
p->data=v;
p->firstarc=NULL;
q=&G->dingdian[u];
while(q->firstarc) q=q->firstarc;
p->firstarc=q->firstarc;
q->firstarc=p;
}
}
void bfs(struct tu *G)
{
di=0;ding=0;
mark[bg]=1;
zhan[ding++]=bg;
int count=0;
while(di!=ding)
{
int ss;
ss=zhan[di];
di++;
if(count==0) {printf("%d",ss);count++;}
else {printf(" %d",ss);count++;}
struct node *p=&G->dingdian[ss];
while(p)
{
if(mark[p->data]==0) //未访问过该节点
{
zhan[ding++]=p->data;
mark[p->data]=1;
}
p=p->firstarc;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&k,&m,&bg);
memset(mark,0,sizeof(mark));
struct tu G;
struct tu *T;
T=NULL;
creat(T); 这里
creat(&G); 和这里
bfs(&G);
printf("\n");
}
}
为什么传递参数时第一个可以,第二个不可以,T和&G不都相当于一个 struct tu *的类型吗
#include<string.h>
#include<malloc.h>
int mark[10060];
int zhan[10060];
int di,ding;
int k,m,bg,u,v;
struct node
{
int data;
struct node *firstarc;
};
struct tu
{
struct node dingdian[200];
int bian;
int dingdiangeshu;
};
void creat(struct tu * &G)
{
int i;
for(i=0;i<=k-1;i++)
{
G->dingdian[i].data=i;
G->dingdian[i].firstarc=NULL;
}
while(m--)
{
// printf("111\n");
scanf("%d%d",&u,&v);
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->data=u;
p->firstarc=NULL;
struct node *q;
q=&G->dingdian[v];
while(q->firstarc) q=q->firstarc;
p->firstarc=q->firstarc;
q->firstarc=p;
p=(struct node *)malloc(sizeof(struct node));
p->data=v;
p->firstarc=NULL;
q=&G->dingdian[u];
while(q->firstarc) q=q->firstarc;
p->firstarc=q->firstarc;
q->firstarc=p;
}
}
void bfs(struct tu *G)
{
di=0;ding=0;
mark[bg]=1;
zhan[ding++]=bg;
int count=0;
while(di!=ding)
{
int ss;
ss=zhan[di];
di++;
if(count==0) {printf("%d",ss);count++;}
else {printf(" %d",ss);count++;}
struct node *p=&G->dingdian[ss];
while(p)
{
if(mark[p->data]==0) //未访问过该节点
{
zhan[ding++]=p->data;
mark[p->data]=1;
}
p=p->firstarc;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&k,&m,&bg);
memset(mark,0,sizeof(mark));
struct tu G;
struct tu *T;
T=NULL;
creat(T); 这里
creat(&G); 和这里
bfs(&G);
printf("\n");
}
}
为什么传递参数时第一个可以,第二个不可以,T和&G不都相当于一个 struct tu *的类型吗