主题:邻接表无向图生成树算法实现疑问
这个程序用来实现深度优先生成树的遍历,
但是总是无法通过遍历生成的二叉链表来打印对应的生成树接点
实在不清楚我那里写错了
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define mvern 20
#define Null 0
#define flase -1
#define truth 1
typedef char vertextype;
typedef struct arcnode
{
int adjvex;
struct arcnode *nextarc;
/* int info;*/
}arcnode;
typedef struct vnode
{
vertextype data[2];
arcnode *firstarc;
}vnode;
typedef struct vnode adjlist[mvern];
typedef struct algraph
{
adjlist vertices;
int vexnum,arcnum;
int kind;
}algraph;
int v,a,first,visited[mvern];
typedef struct csnode
{
int data;
struct csnode *firstch,*nextsibling;
}csnode,*cstree;
void creatal(algraph *x)
{
algraph *g;
arcnode *s;
int i,j,m,n,k;
char c1[2],c2[2];
g=x;
printf("\nPlease input the vexn and arcn:\n");
scanf("%d%d",&g->vexnum,&g->arcnum);
v=g->vexnum;
a=g->arcnum;
printf("g->vexnum=%d g->arcnum=%d\n",g->vexnum,g->arcnum);
fflush(stdin);
printf("\nPlease input the vexdata:\n");
for(i=0;i<g->vexnum;++i)
{
scanf("%s",&g->vertices[i].data);
g->vertices[i].firstarc=Null;
fflush(stdin);
}
for(i=0;i<g->vexnum;++i)
{
printf("%s %d\n",g->vertices[i].data,g->vertices[i].firstarc);
}
printf("\nPlease input the vm->vn:\n");
fflush(stdin);
for(i=0;i<g->arcnum;++i)
{
scanf("%s",&c1);fflush(stdin);
m=locate(g,&c1);
scanf("%s",&c2);
n=locate(g,&c2);
fflush(stdin);
s=(struct arcnode *) malloc (sizeof(struct arcnode));
s->adjvex=n;
s->nextarc=g->vertices[m].firstarc;
g->vertices[m].firstarc=s;
s=(struct arcnode *) malloc (sizeof(struct arcnode));
s->adjvex=m;
s->nextarc=g->vertices[n].firstarc;
g->vertices[n].firstarc=s;
}
}
int locate(algraph *x,char z[2])
{
int i;
for(i=0;i<x->vexnum;++i)
{
if(strcmp((x->vertices[i].data),z)==0)
{
printf("\nlocate i =%d\n",i);
return (i);
break;
}
}
}
void print(algraph *y)
{
algraph *g;
arcnode *p;
int i,j,k;
g=y;
for(k=0;k<g->vexnum;++k)
{
printf("%s->",g->vertices[k].data);
p=g->vertices[k].firstarc;
while(p)
{
printf("%d",p->adjvex);
p=p->nextarc;
if(p!=Null) printf("->");
}
printf("\n");
}
}
dfsforest(algraph *x,csnode *t)
{
algraph *g;
csnode *p,*q;
int vi,i,j,k;
g=x;
/* t=(struct csnode *) malloc (sizeof(struct csnode));*/
t=NULL;
for(vi=0;vi<g->vexnum;vi++)
{
visited[vi]=-1;
}
for(vi=0;vi<g->vexnum;vi++)
{
if(visited[vi]==-1)
{
p=(struct csnode *) malloc (sizeof(struct csnode));
p->data=vi;
printf("\np->data=%d\n",p->data);
p->firstch=NULL;
p->nextsibling=NULL;
if(t==NULL) {t=p;}
else {q->nextsibling=p;}
q=p;
dfstree(g,vi,p);
}
}
}
dfstree(algraph *x,int vi,csnode *t)
{
algraph *g;
arcnode *s;
csnode *p,*q;
int w;
g=x;
visited[vi]=truth;
first=truth;
printf("dfstree visit vertex:%s\n",g->vertices[vi].data);//访问顶点vi
for(w=FirstAdjVex(g,vi);w>=0;w=NextAdjVex(g,vi,w))
{
if(visited[w]==flase)//若vi尚未被访问
{
p=(struct csnode *) malloc (sizeof(struct csnode));
p->data=w;
p->firstch=NULL;
p->nextsibling=NULL;
if(first==truth)
{
t->firstch=p;
first=flase;
}
else{q->nextsibling=p;}
q=p;
dfstree(g,w,q);
}
}
}
int FirstAdjVex(algraph *G,int vi)
{
arcnode *p;
p=G->vertices[vi].firstarc; // vi为顶点在图G中的序号
if(p)
return p->adjvex;
else
return -1;
}
int NextAdjVex(algraph *G,int vi,int w)
{ // 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点
// 操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。
// 若w是v的最后一个邻接点,则返回-1
arcnode *p;
p=G->vertices[vi].firstarc;
while(p&&p->adjvex!=w) // 指针p不空且所指表结点不是w
p=p->nextarc;
if(!p||!p->nextarc) // 没找到w或w是最后一个邻接点
return -1;
else // p->adjvex==w
return p->nextarc->adjvex; // 返回v的(相对于w的)下一个邻接顶点的序号
}
csvisit(csnode *cs)
{
if(cs!=NULL)
{
printf("%d",cs->data);
csvisit(cs->firstch);
csvisit(cs->nextsibling);
}
}
main()
{
algraph *G;
csnode *t;
int i;
G=(struct algraph *) malloc (sizeof(struct algraph));
creatal(G);
print(G);
dfsforest(G,t);
csvisit(t);
}
但是总是无法通过遍历生成的二叉链表来打印对应的生成树接点
实在不清楚我那里写错了
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define mvern 20
#define Null 0
#define flase -1
#define truth 1
typedef char vertextype;
typedef struct arcnode
{
int adjvex;
struct arcnode *nextarc;
/* int info;*/
}arcnode;
typedef struct vnode
{
vertextype data[2];
arcnode *firstarc;
}vnode;
typedef struct vnode adjlist[mvern];
typedef struct algraph
{
adjlist vertices;
int vexnum,arcnum;
int kind;
}algraph;
int v,a,first,visited[mvern];
typedef struct csnode
{
int data;
struct csnode *firstch,*nextsibling;
}csnode,*cstree;
void creatal(algraph *x)
{
algraph *g;
arcnode *s;
int i,j,m,n,k;
char c1[2],c2[2];
g=x;
printf("\nPlease input the vexn and arcn:\n");
scanf("%d%d",&g->vexnum,&g->arcnum);
v=g->vexnum;
a=g->arcnum;
printf("g->vexnum=%d g->arcnum=%d\n",g->vexnum,g->arcnum);
fflush(stdin);
printf("\nPlease input the vexdata:\n");
for(i=0;i<g->vexnum;++i)
{
scanf("%s",&g->vertices[i].data);
g->vertices[i].firstarc=Null;
fflush(stdin);
}
for(i=0;i<g->vexnum;++i)
{
printf("%s %d\n",g->vertices[i].data,g->vertices[i].firstarc);
}
printf("\nPlease input the vm->vn:\n");
fflush(stdin);
for(i=0;i<g->arcnum;++i)
{
scanf("%s",&c1);fflush(stdin);
m=locate(g,&c1);
scanf("%s",&c2);
n=locate(g,&c2);
fflush(stdin);
s=(struct arcnode *) malloc (sizeof(struct arcnode));
s->adjvex=n;
s->nextarc=g->vertices[m].firstarc;
g->vertices[m].firstarc=s;
s=(struct arcnode *) malloc (sizeof(struct arcnode));
s->adjvex=m;
s->nextarc=g->vertices[n].firstarc;
g->vertices[n].firstarc=s;
}
}
int locate(algraph *x,char z[2])
{
int i;
for(i=0;i<x->vexnum;++i)
{
if(strcmp((x->vertices[i].data),z)==0)
{
printf("\nlocate i =%d\n",i);
return (i);
break;
}
}
}
void print(algraph *y)
{
algraph *g;
arcnode *p;
int i,j,k;
g=y;
for(k=0;k<g->vexnum;++k)
{
printf("%s->",g->vertices[k].data);
p=g->vertices[k].firstarc;
while(p)
{
printf("%d",p->adjvex);
p=p->nextarc;
if(p!=Null) printf("->");
}
printf("\n");
}
}
dfsforest(algraph *x,csnode *t)
{
algraph *g;
csnode *p,*q;
int vi,i,j,k;
g=x;
/* t=(struct csnode *) malloc (sizeof(struct csnode));*/
t=NULL;
for(vi=0;vi<g->vexnum;vi++)
{
visited[vi]=-1;
}
for(vi=0;vi<g->vexnum;vi++)
{
if(visited[vi]==-1)
{
p=(struct csnode *) malloc (sizeof(struct csnode));
p->data=vi;
printf("\np->data=%d\n",p->data);
p->firstch=NULL;
p->nextsibling=NULL;
if(t==NULL) {t=p;}
else {q->nextsibling=p;}
q=p;
dfstree(g,vi,p);
}
}
}
dfstree(algraph *x,int vi,csnode *t)
{
algraph *g;
arcnode *s;
csnode *p,*q;
int w;
g=x;
visited[vi]=truth;
first=truth;
printf("dfstree visit vertex:%s\n",g->vertices[vi].data);//访问顶点vi
for(w=FirstAdjVex(g,vi);w>=0;w=NextAdjVex(g,vi,w))
{
if(visited[w]==flase)//若vi尚未被访问
{
p=(struct csnode *) malloc (sizeof(struct csnode));
p->data=w;
p->firstch=NULL;
p->nextsibling=NULL;
if(first==truth)
{
t->firstch=p;
first=flase;
}
else{q->nextsibling=p;}
q=p;
dfstree(g,w,q);
}
}
}
int FirstAdjVex(algraph *G,int vi)
{
arcnode *p;
p=G->vertices[vi].firstarc; // vi为顶点在图G中的序号
if(p)
return p->adjvex;
else
return -1;
}
int NextAdjVex(algraph *G,int vi,int w)
{ // 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点
// 操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。
// 若w是v的最后一个邻接点,则返回-1
arcnode *p;
p=G->vertices[vi].firstarc;
while(p&&p->adjvex!=w) // 指针p不空且所指表结点不是w
p=p->nextarc;
if(!p||!p->nextarc) // 没找到w或w是最后一个邻接点
return -1;
else // p->adjvex==w
return p->nextarc->adjvex; // 返回v的(相对于w的)下一个邻接顶点的序号
}
csvisit(csnode *cs)
{
if(cs!=NULL)
{
printf("%d",cs->data);
csvisit(cs->firstch);
csvisit(cs->nextsibling);
}
}
main()
{
algraph *G;
csnode *t;
int i;
G=(struct algraph *) malloc (sizeof(struct algraph));
creatal(G);
print(G);
dfsforest(G,t);
csvisit(t);
}