马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#define MAXVERTEX 20
typedef char VertexType;
typedef int EdgeType;
typedef struct EdgeNode
{
int adjvex; //要存放的下标
EdgeType weight;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
struct EdgeNode *firstedge;
}VertexNode,AdjList[MAXVERTEX];
typedef struct GraphAdjList
{
AdjList adjList;
int numVertexAdjList;
int numEdgeAdjList;
}GraphAdjList;
static int Flag[MAXVERTEX];
//建造一个图
GraphAdjList* CreateAdjList(GraphAdjList *G)
{
int i = 0,j = 0;
EdgeNode *e;
VertexType c;
printf("请输入图的顶点数和边数,中间用逗号隔开:\n");
fflush(stdin);
scanf(" %d,%d",&(G->numVertexAdjList),&(G->numEdgeAdjList));
printf("请输入顶点的值 :\n");
scanf("%c",&c);
while(i < G->numVertexAdjList)
{
if(c == '\n')
break;
G->adjList[i].data = c;
G->adjList[i].firstedge = NULL;
scanf("%c",&c);
}
for(i = 0;i < G->numEdgeAdjList;i++)
{
printf("请输入边(Vi~Vj)的邻接顶点的下标:");
scanf("%d,%d",i,j);
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = j;
e->next = G->adjList[i].firstedge;
G->adjList[i].firstedge = e;
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = i;
e->next = G->adjList[j].firstedge;
G->adjList[j].firstedge = e;
}
return G;
}
//给标识数组赋初值0
void InitFlag()
{
int i;
for(i = 0;i < MAXVERTEX;i++)
{
Flag[i] = 0;
}
}
void DepthFirstSearch(GraphAdjList *GL,int i)
{
int j = 0;
EdgeNode *p;
p = GL->adjList[i].firstedge;
Flag[i] = 1; //赋值为1说明已经遍历过
printf("%c ",GL->adjList[i].data);
while(p != NULL)
{
if(Flag[p->adjvex] == 0)
{
DepthFirstSearch(GL,p->adjvex);
}
p = p->next;
}
}
int main()
{
int k = 0;
GraphAdjList *G;
GraphAdjList *GL;
InitFlag();
fflush(stdin);
GL = CreateAdjList(G);
DepthFirstSearch(GL,k);
return 0;
}
请问一下 scanf(" %d,%d",&(G->numVertexAdjList),&(G->numEdgeAdjList));
这里我输入
4,5回车
为什么会报错呢?
|