图的练习创建

不知道为什么会出错

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值