不知道为什么会出错
#include<stdio.h>
#define MAX 100
#define GRAPH 30
#define UNDIGRA GRAPH + 0
#define DIGRA GRAPH + 1
#define NET 50
#define UNDINET NET + 0
#define DINET NET + 1
#define INFINITY 5000
typedef char VertexType;
typedef int EdgeType;
typedef struct
{
VertexType vexs[MAX];
EdgeType edges[MAX][MAX];
int vexnum,arcnum;
}AdjMatrix;
AdjMatrix CreateGraph_Matrix(int graphtype)
{
AdjMatrix G;
int i,j,k;
int weight;
VertexType ch;
printf("please input grath's vexnum and arcnum:\n");
scanf("%d,%d",&G.vexnum,&G.arcnum);
for(i=1;i<=4;i++)
printf("%c",G.vexs[i]);
for(i=1;i<=G.vexnum;i++)
{
scanf("%c",&G.vexs[i]);
printf("%c\n",G.vexs[i]);
}
if(graphtype - GRAPH == 0 || graphtype - GRAPH == 1)
{
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.edges[i][j]=0;
for(k=1;k<=G.arcnum;k++)
{
printf("please input NO.%d edge's start and end:\n",k);
scanf("%d,%d",&i,&j);
G.edges[i][j]=1;
if(UNDIGRA == graphtype)
G.edges[j][i]=1;
}
}
if(graphtype - NET == 0 || graphtype - NET == 1)
{
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.edges[i][j]=INFINITY;
for(k=1;k<=G.arcnum;k++)
{
printf("please input NO.%d edge's start,end and weight:\n",k);
scanf("%d,%d,%d",&i,&j,&weight);
G.edges[i][j]=weight;
if(UNDINET == graphtype)
G.edges[j][i]=weight;
}
}
return(G);
}
int main()
{
AdjMatrix g;
g=CreateGraph_Matrix(30);
printf("%d",g.vexnum);
printf("%d",g.arcnum);
printf("%c\n",g.vexs[1]);
printf("ok!\n");
return 0;
}
经过检查,发现scanf函数的问题,故转载一篇关于scanf的文章,现在将没有问题的贴如下,注意我改的地方(仔细找哦!)
#include<stdio.h>
#define MAX 100
#define GRAPH 30
#define UNDIGRA GRAPH + 0
#define DIGRA GRAPH + 1
#define NET 50
#define UNDINET NET + 0
#define DINET NET + 1
#define INFINITY 5000
typedef char VertexType;
typedef int EdgeType;
typedef struct
{
VertexType vexs[MAX];
EdgeType edges[MAX][MAX];
int vexnum,arcnum;
}AdjMatrix;
AdjMatrix CreateGraph_Matrix(int graphtype)
{
AdjMatrix G;
int i,j,k;
int weight;
VertexType ch;
printf("please input grath's vexnum and arcnum:\n");
scanf("%d,%d",&G.vexnum,&G.arcnum);
fflush(stdin);
for(i=1;i<=G.vexnum;i++)
{
scanf("%c",&G.vexs[i]);
fflush(stdin);
}
if(graphtype - GRAPH == 0 || graphtype - GRAPH == 1)
{
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.edges[i][j]=0;
for(k=1;k<=G.arcnum;k++)
{
printf("please input NO.%d edge's start and end:\n",k);
scanf("%d,%d",&i,&j);
G.edges[i][j]=1;
if(UNDIGRA == graphtype)
G.edges[j][i]=1;
}
}
if(graphtype - NET == 0 || graphtype - NET == 1)
{
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.edges[i][j]=INFINITY;
for(k=1;k<=G.arcnum;k++)
{
printf("please input NO.%d edge's start,end and weight:\n",k);
scanf("%d,%d,%d",&i,&j,&weight);
G.edges[i][j]=weight;
if(UNDINET == graphtype)
G.edges[j][i]=weight;
}
}
return(G);
}
int main()
{
AdjMatrix g;
g=CreateGraph_Matrix(30);
printf("%d",g.vexnum);
printf("%d",g.arcnum);
printf("%c\n",g.vexs[1]);
printf("ok!\n");
return 0;
}
问题解决完毕
函数名: fflush
功 能:
清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
头文件:<stdio.h>
原型:int fflush(FILE *stream)
邻接表创建
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define GRAPH 30
#define UNDIGRA GRAPH + 0
#define DIGRA GRAPH + 1
#define DIGRA_REV GRAPH + 2
#define NET 50
#define UNDINET NET + 0
#define DINET NET + 1
#define INFINITY 5000
typedef char VertexType;
typedef int EdgeType;
typedef struct EdgeNode
{
int adjno;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
EdgeNode * first;
}VertexNode;
typedef struct
{
VertexNode vexs[MAX];
int vexnum,arcnum;
}AdjList;
AdjList CreateGraph_List(int graphtype)
{
AdjList G;
int i,s,d;
EdgeNode *p,*q;
printf("please input graph's vexnum and arcnum:\n");
scanf("%d,%d",&G.vexnum,&G.arcnum);
fflush(stdin);
for(i=1;i<=G.vexnum;i++)
{
printf("please input NO.%d vexs information:\n",i);
scanf("%c",&G.vexs[i].data);
fflush(stdin);
G.vexs[i].first=NULL;
}
for(i=1;i<=G.arcnum;i++)
{
printf("please input NO.%d edge's start and end:\n",i);
scanf("%d,%d",&s,&d);
fflush(stdin);
if(UNDINET == graphtype || DIGRA_REV == graphtype)
{
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjno=s;
p->next=G.vexs[d].first;
G.vexs[d].first=p;
}
if(UNDINET == graphtype || DINET == graphtype)
{
q=(EdgeNode *)malloc(sizeof(EdgeNode));
q->adjno=d;
q->next=G.vexs[s].first;
G.vexs[d].first=q;
}
}
return(G);
}
int main()
{
AdjList g;
g=CreateGraph_List(50);
return 0;
}