图的广度优先遍历为什么数据多了之后就出现错误现象?求高手帮忙
我的代码是:#include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>typedefst...
我的代码是:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{int data;
struct node *next;
}Snode;
typedef struct
{Snode *front,*rear;
}Queue;
typedef struct Arcbox
{int tailvex,headvex;
struct Arcbox *tlink,*hlink;
}Arcbox;
typedef struct Vexnode
{ char data;
Arcbox *firstout,*firstin;
}Vexnode;
typedef struct OLGraph
{Vexnode xlist[100];
int vexnum,arcnum;
}OLGraph;
int visit[100];
int locate(OLGraph g,char v);
void createGraph(OLGraph &g);
int FirstAdjVex(OLGraph g,int v);
int NextAdjVex(OLGraph g,int v,int w);
void InitQueue(Queue &q);
void EnQuquq(Queue *&q,int v);
int DeQueue(Queue *&q);
int Empty(Queue *q);
void bfs(OLGraph g,int v);
void bfstraverse(OLGraph g);
int main()
{OLGraph g;
createGraph(g);
printf("\n广度优化遍历:\n");
bfstraverse(g);
system("pause");
return 0;
}
void InitQueue(Queue *q)
{Snode *h;
h=(Snode *)malloc(sizeof(Snode));
h->next=NULL;
q->front=h;
q->rear=h;
}
void EnQueue(Queue *q,int v)
{Snode *s;s=(Snode *)malloc(sizeof(Snode));s->data=v;s->next=NULL; q->rear->next=s;q->rear=s; }
int DeQueue(Queue *&q)
{int x;Snode *s;if (q->front==q->rear)
return 0;else { x=q->front->next->data;
s=q->front->next;q->front->next=s->next;if(s->next==NULL)
q->front=q->rear;free(s);return x;}}
int Empty(Queue *q)
{if (q->front==q->rear)
return (1);else
return (0);
}
int locate(OLGraph g,char v)
{int i=0;while (g.xlist[i].data!=v)
{i++;if(i>g.vexnum)
return -1;}return i;
}void createGraph(OLGraph &g)
{int i;printf("输入节点的个数:");scanf("%d",&g.vexnum);printf("输入路径个数:");scanf("%d",&g.arcnum);
getchar();for (i=1;i<=g.vexnum;i++)
{printf("\n输入第%d个节点的数据:",i);
scanf("%s",&g.xlist[i].data);
g.xlist[i].firstout=NULL;
g.xlist[i].firstin=NULL;
}int j,k; char v1,v2; Arcbox *p;for (k=1;k<=g.arcnum;k++)
{printf("\n输入第%d个路径的信息(尾结点、头结点):",k);
v1=getche();
v2=getche();
i=locate(g,v1);
j=locate(g,v2);
p=(Arcbox *)malloc(sizeof(Arcbox));
p->tailvex=i;
p->headvex=j;
p->tlink=g.xlist[i].firstout;
p->hlink=g.xlist[j].firstin;
g.xlist[i].firstout=g.xlist[j].firstin=p;
}}int FirstAdjVex(OLGraph g,int v)
{if(g.xlist[v].firstout!=NULL)
return g.xlist[v].firstout->headvex;
else
return -1;
}int NextAdjVex(OLGraph g,int v,int w)
{Arcbox *p;
p=g.xlist[v].firstout;
while (p->headvex!=w{
p=p->tlink;
}if(p->tlink==NULL)
return -1;
else
return p->tlink->headvex;}
void bfs(OLGraph g,int v)
{int u;
Queue *q;
q=(Queue *)malloc(sizeof(Queue));
InitQueue(q);
for (v=1;v<=g.vexnum;v++)
{if (!visit[v])
{printf("%c ",g.xlist[v].data);
visit[v]=1;
EnQueue(q,v);
while (!Empty(q))
{u=DeQueue(q);
for (int w=FirstAdjVex(g,u);w>=1;w=NextAdjVex(g,u,w))
{
if (!visit[w])
{visit[w]=1;
printf("%c ",g.xlist[w].data);
EnQueue(q,w);
}
}
}
}
}
}
void bfstraverse(OLGraph g){int v;
for(v=1;v<=g.vexnum;v++)
visit[v]=0;
for (v=1;v<=g.vexnum;v++)
if (!visit[v])
bfs(g,v);
} 展开
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{int data;
struct node *next;
}Snode;
typedef struct
{Snode *front,*rear;
}Queue;
typedef struct Arcbox
{int tailvex,headvex;
struct Arcbox *tlink,*hlink;
}Arcbox;
typedef struct Vexnode
{ char data;
Arcbox *firstout,*firstin;
}Vexnode;
typedef struct OLGraph
{Vexnode xlist[100];
int vexnum,arcnum;
}OLGraph;
int visit[100];
int locate(OLGraph g,char v);
void createGraph(OLGraph &g);
int FirstAdjVex(OLGraph g,int v);
int NextAdjVex(OLGraph g,int v,int w);
void InitQueue(Queue &q);
void EnQuquq(Queue *&q,int v);
int DeQueue(Queue *&q);
int Empty(Queue *q);
void bfs(OLGraph g,int v);
void bfstraverse(OLGraph g);
int main()
{OLGraph g;
createGraph(g);
printf("\n广度优化遍历:\n");
bfstraverse(g);
system("pause");
return 0;
}
void InitQueue(Queue *q)
{Snode *h;
h=(Snode *)malloc(sizeof(Snode));
h->next=NULL;
q->front=h;
q->rear=h;
}
void EnQueue(Queue *q,int v)
{Snode *s;s=(Snode *)malloc(sizeof(Snode));s->data=v;s->next=NULL; q->rear->next=s;q->rear=s; }
int DeQueue(Queue *&q)
{int x;Snode *s;if (q->front==q->rear)
return 0;else { x=q->front->next->data;
s=q->front->next;q->front->next=s->next;if(s->next==NULL)
q->front=q->rear;free(s);return x;}}
int Empty(Queue *q)
{if (q->front==q->rear)
return (1);else
return (0);
}
int locate(OLGraph g,char v)
{int i=0;while (g.xlist[i].data!=v)
{i++;if(i>g.vexnum)
return -1;}return i;
}void createGraph(OLGraph &g)
{int i;printf("输入节点的个数:");scanf("%d",&g.vexnum);printf("输入路径个数:");scanf("%d",&g.arcnum);
getchar();for (i=1;i<=g.vexnum;i++)
{printf("\n输入第%d个节点的数据:",i);
scanf("%s",&g.xlist[i].data);
g.xlist[i].firstout=NULL;
g.xlist[i].firstin=NULL;
}int j,k; char v1,v2; Arcbox *p;for (k=1;k<=g.arcnum;k++)
{printf("\n输入第%d个路径的信息(尾结点、头结点):",k);
v1=getche();
v2=getche();
i=locate(g,v1);
j=locate(g,v2);
p=(Arcbox *)malloc(sizeof(Arcbox));
p->tailvex=i;
p->headvex=j;
p->tlink=g.xlist[i].firstout;
p->hlink=g.xlist[j].firstin;
g.xlist[i].firstout=g.xlist[j].firstin=p;
}}int FirstAdjVex(OLGraph g,int v)
{if(g.xlist[v].firstout!=NULL)
return g.xlist[v].firstout->headvex;
else
return -1;
}int NextAdjVex(OLGraph g,int v,int w)
{Arcbox *p;
p=g.xlist[v].firstout;
while (p->headvex!=w{
p=p->tlink;
}if(p->tlink==NULL)
return -1;
else
return p->tlink->headvex;}
void bfs(OLGraph g,int v)
{int u;
Queue *q;
q=(Queue *)malloc(sizeof(Queue));
InitQueue(q);
for (v=1;v<=g.vexnum;v++)
{if (!visit[v])
{printf("%c ",g.xlist[v].data);
visit[v]=1;
EnQueue(q,v);
while (!Empty(q))
{u=DeQueue(q);
for (int w=FirstAdjVex(g,u);w>=1;w=NextAdjVex(g,u,w))
{
if (!visit[w])
{visit[w]=1;
printf("%c ",g.xlist[w].data);
EnQueue(q,w);
}
}
}
}
}
}
void bfstraverse(OLGraph g){int v;
for(v=1;v<=g.vexnum;v++)
visit[v]=0;
for (v=1;v<=g.vexnum;v++)
if (!visit[v])
bfs(g,v);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询