数据结构07图的邻接矩阵表示

数据结构07图的邻接矩阵表示

提示:以下是本篇文章正文内容,下面案例可供参考

一、主函数 main.cpp

#include"graphmtx.h"

void main()
{
	graphmtx gm;
	initgraph(&gm);
	insertvertice(&gm,'A');
	insertvertice(&gm,'B');
	insertvertice(&gm,'C');
	insertvertice(&gm,'D');
	insertvertice(&gm,'E');
	insertedge(&gm,'A','B');
	insertedge(&gm,'A','D');
	insertedge(&gm,'C','B');
	insertedge(&gm,'E','B');
	insertedge(&gm,'D','C');
	insertedge(&gm,'C','E');
	showgraph(&gm);

//	removeedge(&gm,'C','E');
//	showgraph(&gm);

//	removevertice1(&gm,'C');
//	showgraph(&gm);

//	removevertice2(&gm,'C');
//	showgraph(&gm);

	int i=getfirstneighbor(&gm,'A');
	printf("%d",i);
	printf("\n");
	int j=getnextneighbor(&gm,'A','B');
	printf("%d",j);
	printf("\n");
}

二、函数内容实现 graphmtx.cpp

#include"graphmtx.h"

void initgraph(graphmtx *g)
{
	g->maxvertice=defaultnum;
	g->numedge=g->numvertice=0;

	g->verticelist=(t*)malloc(sizeof(t)*defaultnum);
	assert(g->verticelist !=NULL);

	g->edge=(int **)malloc(sizeof(int *)*defaultnum);
	assert(g->edge!=NULL);

	for(int i=0;i<g->maxvertice;++i)
	{
		g->edge[i]=(int *)malloc(sizeof(int)*defaultnum);
	}
	for(i=0;i<g->maxvertice;++i)
	{
		for(int j=0;j<g->maxvertice;++j)
		{
			g->edge[i][j]=0;
		}
	}
}



void showgraph(graphmtx *g)
{
	printf("  ");
	for(int i=0;i<g->numvertice;i++)
		printf("%c ",g->verticelist[i]);
	printf("\n");
	for(i=0;i<g->numvertice;i++)
	{
		printf("%c ",g->verticelist[i]);
		for(int j=0;j<g->numvertice;j++)
		{
			printf("%d ",g->edge[i][j]);
		}
		printf("\n");
	}
	printf("\n");

}


void insertvertice(graphmtx *g,t e)
{
	if(g->numedge>g->numvertice)
	{
		printf("已满");
		return;
	}
	g->verticelist[g->numvertice++]=e;

}


int findpos(graphmtx *g,t e)
{
	for(int i=0;i<g->numvertice;i++)
	{
		if(g->verticelist[i]==e)
			return i;
	}
	return -1;
}



void insertedge(graphmtx *g,t e1,t e2)
{
	int p=findpos(g,e1);
	int w=findpos(g,e2);
	if(p==-1||w==-1)
		return;
	if(g->edge[p][w]!=0)
		return;
	g->edge[p][w]=g->edge[w][p]=1;
}


void removeedge(graphmtx *g,t e1,t e2)
{
	int p=findpos(g,e1);
	int w=findpos(g,e2);
	if(p==-1||w==-1)
		return;
	if(g->edge[p][w]==0)
		return;
	g->edge[p][w]=g->edge[w][p]=0;
}

void removevertice1(graphmtx *g,t e)
{
	int num=0;
	int p=findpos(g,e);
	if(p==-1)
		return;
	for(int i=0;i<g->numvertice;i++)
	{
		if(g->edge[p][i]!=0)
			num++;
	}
	for(i=p;i<g->numvertice;i++)
	{
		g->verticelist[i]=g->verticelist[i+1];
		for(int j=0;j<g->numvertice;j++)
		{
			g->edge[i][j]=g->edge[i+1][j];
		}
	}
	for(i=0;i<g->numvertice-1;i++)
	{
		for(int j=p;j<g->numvertice;j++)
		{
			g->edge[i][j]=g->edge[i][j+1];
		}
	}
	g->numedge-=num;
	g->numvertice--;
}
void removevertice2(graphmtx *g,t e)
{
	int num=0;
	int p=findpos(g,e);
	if(p==-1)
		return;
	g->verticelist[p]=g->verticelist[g->numvertice-1];
	for(int i=0;i<g->numvertice;i++)
	{
		if(g->edge[p][i]!=0)
			num++;
	}
	for(i=0;i<g->numvertice;i++)
	{
		g->edge[p][i]=g->edge[g->numvertice-1][i];
		g->edge[g->numvertice-1][i]=0;
	}
	for(i=0;i<g->numvertice-1;i++)
	{
		g->edge[i][p]=g->edge[i][g->numvertice-1];
		g->edge[i][g->numvertice-1]=0;
	}

	g->numedge-=num;
	g->numvertice--;
}

void destroygraph(graphmtx *g)
{
	free(g->verticelist);
	g->verticelist=	NULL;
	for(int i=0;i<g->numvertice;i++)
	{
		free(g->edge[i]);
		g->edge[i]=NULL;
	}
	free(g->edge);
	g->maxvertice=g->numedge=g->numvertice=0;
}
int getfirstneighbor(graphmtx *g,t e)
{
	int p=findpos(g,e);
	for(int i=0;i<g->numvertice;i++)
	{
		if(g->edge[p][i]==1)
			return i;
	}
	return -1;
}
int getnextneighbor(graphmtx *g,t e1,t e2)
{
	int p=findpos(g,e1);
	int w=findpos(g,e2);
	for(int i=w+1;i<g->numvertice;i++)
	{
		if(g->edge[p][i]==1)
			return i;
	}
	return -1;
}

三、头文件的引入(相关结构的定义)graphmtx.h

#pragma once
#include<stdio.h>
#include<malloc.h>
#include<assert.h>

#define defaultnum 10
#define t char

typedef struct graphmtx
{
	int maxvertice;
	int numvertice;
	int numedge;

	t *verticelist;
	int **edge;
}graphmtx;


void initgraph(graphmtx *g);
void showgraph(graphmtx *g);
void insertvertice(graphmtx *g,t e);
int findpos(graphmtx *g,t e);
void insertedge(graphmtx *g,t e1,t e2);
void removeedge(graphmtx *g,t e1,t e2);
void removevertice1(graphmtx *g,t e);
void removevertice2(graphmtx *g,t e);
void destroygraph(graphmtx *g);
int getfirstneighbor(graphmtx *g,t e);
int getnextneighbor(graphmtx *g,t e1,t e2);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值