平时笔记——1

第二次

#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 enum{FALSE,TRUE} VisitFlag;
VisitFlag visited[MAX];
typedef struct
{
	VertexType vexs[MAX];
	EdgeType edges[MAX][MAX];
	int vexnum,arcnum;
}AdjMatrix;
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);
}
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);
}
void DFS_Matrix(AdjMatrix G,int k)
{
	int j;
	printf("Visit vertex: %c \n",G.vexs[k]);
	visited[k]=TRUE;
	for(j=1;j<=G.vexnum;j++)
	{
		if((1 == G.edges[k][j]) && (FALSE==visited[j]))
			DFS_Matrix(G,k); 
	}
}
void DFSTraverse_Matrix(AdjMatrix G)
{
	int k;
	for(k=1;k<=G.vexnum;k++)
		visited[k]=FALSE;
	for(k=1;k<=G.vexnum;k++)
	{
		if(FALSE==visited[k])
			DFS_Matrix(G,k);
	}
}
int main()
{
	AdjMatrix CreateGraph_Matrix(int graphtype);
	AdjList CreateGraph_List(int graphtype);
	void DFS_Matrix(AdjMatrix G,int k);
	void DFSTraverse_Matrix(AdjMatrix G);
	AdjMatrix g;
	g=CreateGraph_Matrix(30);
	printf("%d",g.vexnum);
	printf("%d",g.arcnum);
	printf("%c\n",g.vexs[2]);
	printf("ok!\n");
	DFSTraverse_Matrix(g);
	return 0;
}

出现问题,修改如下

#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 enum{FALSE,TRUE} VisitFlag;
VisitFlag visited[MAX];
typedef struct
{
	VertexType vexs[MAX];
	EdgeType edges[MAX][MAX];
	int vexnum,arcnum;
}AdjMatrix;
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);
}
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);
}
void DFS_Matrix(AdjMatrix G,int k)
{
	int j;
	printf("Visit NO.%d vertex: %c \n",k,G.vexs[k]);
	visited[k]=TRUE;
	for(j=1;j<=G.vexnum;j++)
	{
		if(1 == G.edges[k][j] && FALSE==visited[j])
			DFS_Matrix(G,j); 
	}
}
void DFSTraverse_Matrix(AdjMatrix G)
{
	int k;
	for(k=1;k<=G.vexnum;k++)
		visited[k]=FALSE;
	for(k=1;k<=G.vexnum;k++)
	{
		if(FALSE==visited[k])
			DFS_Matrix(G,k);
	}
}
int main()
{
	AdjMatrix CreateGraph_Matrix(int graphtype);
	AdjList CreateGraph_List(int graphtype);
	void DFS_Matrix(AdjMatrix G,int k);
	void DFSTraverse_Matrix(AdjMatrix G);
	AdjMatrix g;
	g=CreateGraph_Matrix(30);
	printf("%d",g.vexnum);
	printf("%d",g.arcnum);
	printf("%c\n",g.vexs[2]);
	printf("ok!\n");
	DFSTraverse_Matrix(g);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值