主题:帮帮忙
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define M 100
typedef struct st
{
int adjvex;
struct st *next;
}st;
typedef struct
{char vertex;
st *firstedge;
}Li;
typedef struct graph
{Li adjlist[M];
int vertexnum,arcnum;
}G;
G *g;
int visited[M];
void MGraph(G *g,Li *L,char a[],int n,int e)//创建以邻接矩阵作存储结构的无向图
{ int i,j,k;
st *t;
g->vertexnum=n;g->arcnum=e;
for(i=0;i<n;i++)
g->adjlist[i].vertex=a[i];
g->adjlist[i].firstedge=NULL;
for(k=0;k<e;k++)
{ printf("请输入依附边的两个顶点:\n");
scanf("%d%d",&i,&j);
t=(st *)malloc(sizeof(st));
t->adjvex=j;
t->next=g->adjlist[i].firstedge;
g->adjlist[i].firstedge=t;
}
}
void rebuild(G *g,st *s,Li *L,int n)
{ int i;
for(i=0;i<n;i++)
visited[i]=0;
}
void dfstraverse(G *g,st *s,Li *L,int i)//深度优先遍历
{st *p;
int j;
printf("%3c",g->adjlist[i].vertex);
p=(st *)malloc(sizeof(st));
visited[i]=1;
p=g->adjlist[i].firstedge;
while(p)
{
j=p->adjvex;
if(visited[j]==0)
dfstraverse(g,s,L,j);
p=p->next;
}
}
void bfstraverse(G *g,st *s,Li *L,int i)//广度优先遍历
{ st *p;
int Q[M],j=0;
char front,rear;
front=rear=-1;
printf("%c",g->adjlist[i].vertex);
visited[i]=1;
Q[++rear]=i;
while(front!=rear)
{ i=Q[++front];
p=g->adjlist[i].firstedge;
while(p)
{
j=p->adjvex;
if(visited[j]==0)
{
printf("%3c",g->adjlist[j].vertex);
visited[j]=1;
Q[++rear]=j;
}
p=p->next;
}
}
}
main()//主函数
{ st *s;
Li *L;
G *g;
int n=0,e=0,k=0;
char a[10];
printf("请输入字符串:\n");
scanf("%s",&a);
g=(G *)malloc(sizeof(g));
s=(st *)malloc(sizeof(st));
L=(Li *)malloc(sizeof(Li));
while(k!=4)
{ printf("请选择你要进行的操作:\n");
printf("1:创建无向图!\n");
printf("2:深度优先遍历无向图!\n");
printf("3:广度优先遍历无向图!\n");
printf("4:退出程序\n");
scanf("%d",&k);
switch (k)
{ case 1:printf("输入你所需要构建的图的顶点数:\n");
scanf("%d",&n);
printf("输入你所需要构建的图的边数:\n");
scanf("%d",&e);
MGraph(g,L,a,n,e);
printf("\n");
break;
case 2:rebuild(g,s,L,n);
dfstraverse(g,s,L,0);
printf("\n");
break;
case 3:rebuild(g,s,L,n);
bfstraverse(g,s,L,0);
printf("\n");
break;
case 4:break;
}
}
}
运行时出故障
#include <stdlib.h>
#include <malloc.h>
#define M 100
typedef struct st
{
int adjvex;
struct st *next;
}st;
typedef struct
{char vertex;
st *firstedge;
}Li;
typedef struct graph
{Li adjlist[M];
int vertexnum,arcnum;
}G;
G *g;
int visited[M];
void MGraph(G *g,Li *L,char a[],int n,int e)//创建以邻接矩阵作存储结构的无向图
{ int i,j,k;
st *t;
g->vertexnum=n;g->arcnum=e;
for(i=0;i<n;i++)
g->adjlist[i].vertex=a[i];
g->adjlist[i].firstedge=NULL;
for(k=0;k<e;k++)
{ printf("请输入依附边的两个顶点:\n");
scanf("%d%d",&i,&j);
t=(st *)malloc(sizeof(st));
t->adjvex=j;
t->next=g->adjlist[i].firstedge;
g->adjlist[i].firstedge=t;
}
}
void rebuild(G *g,st *s,Li *L,int n)
{ int i;
for(i=0;i<n;i++)
visited[i]=0;
}
void dfstraverse(G *g,st *s,Li *L,int i)//深度优先遍历
{st *p;
int j;
printf("%3c",g->adjlist[i].vertex);
p=(st *)malloc(sizeof(st));
visited[i]=1;
p=g->adjlist[i].firstedge;
while(p)
{
j=p->adjvex;
if(visited[j]==0)
dfstraverse(g,s,L,j);
p=p->next;
}
}
void bfstraverse(G *g,st *s,Li *L,int i)//广度优先遍历
{ st *p;
int Q[M],j=0;
char front,rear;
front=rear=-1;
printf("%c",g->adjlist[i].vertex);
visited[i]=1;
Q[++rear]=i;
while(front!=rear)
{ i=Q[++front];
p=g->adjlist[i].firstedge;
while(p)
{
j=p->adjvex;
if(visited[j]==0)
{
printf("%3c",g->adjlist[j].vertex);
visited[j]=1;
Q[++rear]=j;
}
p=p->next;
}
}
}
main()//主函数
{ st *s;
Li *L;
G *g;
int n=0,e=0,k=0;
char a[10];
printf("请输入字符串:\n");
scanf("%s",&a);
g=(G *)malloc(sizeof(g));
s=(st *)malloc(sizeof(st));
L=(Li *)malloc(sizeof(Li));
while(k!=4)
{ printf("请选择你要进行的操作:\n");
printf("1:创建无向图!\n");
printf("2:深度优先遍历无向图!\n");
printf("3:广度优先遍历无向图!\n");
printf("4:退出程序\n");
scanf("%d",&k);
switch (k)
{ case 1:printf("输入你所需要构建的图的顶点数:\n");
scanf("%d",&n);
printf("输入你所需要构建的图的边数:\n");
scanf("%d",&e);
MGraph(g,L,a,n,e);
printf("\n");
break;
case 2:rebuild(g,s,L,n);
dfstraverse(g,s,L,0);
printf("\n");
break;
case 3:rebuild(g,s,L,n);
bfstraverse(g,s,L,0);
printf("\n");
break;
case 4:break;
}
}
}
运行时出故障