数据结构与算法关于图的邻接矩阵部分相关代码

//****以下是数据结构中有关邻接矩阵的内容与知识******//
//****邻接矩阵的头文件***//
#pragma once
#include "stdio.h"
#include "malloc.h"
#include "assert.h"
#include "stdlib.h"
#define MAXVETICE 10
#define T char
typedef struct MGRAPh
{
int MaxVextices;//最大可申请的顶点数
int Vextices;//当前顶点数
int Arcs;//邻接矩阵的边数
T *vexlist;//顶点集
int **p;//边集合
}MGraph;//别名
void InitMgraph(MGraph *G);//初始化邻接矩阵
int locate(MGraph *g,T e);//根据给定符号找下标
void InsertMG(MGraph* g,T e);//插入顶点函数
void Insertedge(MGraph*g,T v1,T v2);//插入边的函数
void showMG(MGraph* g);//打印临界矩阵
void removeEdge(MGraph* g,T v1,T v2);//删除邻接矩阵的一条边
void removeVertice(MGraph *g,T e);//删除邻接矩阵的一个顶点
void DestroyMG(MGraph *g);
char FindNeibro(MGraph *g,T e);
char FindnextNeibro(MGraph *g,T v1,T v2);
//***************************************************************************//
//****邻接矩阵的函数定义***//
#include "MGraph.h"
void InitMgraph(MGraph *G)
{
int i=0;
int j=0;
G->MaxVextices=MAXVETICE;//把最大顶点的数量复制成MAX 10
G->Arcs=0;//变数初始化为0
G->Vextices=0;//顶点数初始化为零
G->vexlist=(T*)malloc(sizeof(T)*MAXVETICE);//向空间开辟一个顶点集合
G->p=(int**)malloc(sizeof(int*)*MAXVETICE);//二级指针开辟空间//先拨开一行
for (i=0;i<MAXVETICE;i++)
{
G->p[i]=(int*)malloc(sizeof(int)*MAXVETICE);
}//写一个二维输注形式的循环要空间
for(i=0;i<G->MaxVextices;i++)//初始化索要空间数组的之为0
{
for (j=0;j<G->MaxVextices;j++)
{
G->p[i][j]=0;
}
}
}
int locate(MGraph *g,T e)//传进来结构体和顶点
{
int i=0;
for(i=0;i<g->Vextices;i++)
{
if (g->vexlist[i]==e)
return i;//如过找到了个顶点在那个地方标一个数i;
}
return -1;//找不到返回-1,要注意一定要吧return -1放在循环之外
}
void InsertMG(MGraph* g,T e)//插入一个顶点
{
if (g->MaxVextices<=g->Vextices)//先判断
{
;
}
else
g->vexlist[g->Vextices++]=e;//将e赋值给 g.vexlist[g.Vextices++]这段代码十分的巧妙
}
void Insertedge(MGraph*g,T v1,T v2)
{
int p1=locate(g,v1);
int p2=locate(g,v2);
if (p1==-1||p2==-1)
return;
g->p[p2][p1]=g->p[p1][p2]=1;
g->Arcs++;
}
void showMG(MGraph* g)
{
int i=0;
int j=0;
printf(" ");
for (i=0;i<g->Vextices;i++)
{
printf("%c ",g->vexlist[i]);
}
printf("\n");
for (i=0;i<g->Vextices;i++)
{
printf("%c ",g->vexlist[i]);
for(j=0;j<g->Vextices;j++)
{
printf("%d ",g->p[i][j]);
}
printf("\n");
}
printf("\n");
}
void removeEdge(MGraph* g,T v1,T v2)
{
int p1=locate(g,v1);
int p2=locate(g,v2);
if (p1==-1||p2==-1)
{
;
}
g->p[p1][p2]=0;
g->p[p2][p1]=0;
g->Arcs--;
}
void removeVertice(MGraph *g,T e)
{
int num=0;
int i=0;
int j=0;
int p1=0;
p1=locate(g,e);
if (p1==-1)
return;
for (i=p1;i<g->Vextices-1;i++)
{
g->vexlist[i]=g->vexlist[i+1];
}
for(i=p1;i<g->Vextices;i++)
{
if(g->p[p1][i]!=0)
{
num++;
}
}
for (i=p1;i<g->Vextices-1;i++)
{
for (j=0;j<g->Vextices;j++)
{
g->p[i][j]=g->p[i+1][j];
}
}
for (i=p1;i<g->Vextices-1;i++)
{
for (j=0;j<g->Vextices-1;j++)
{
g->p[j][i]=g->p[j][i+1];
}
}
g->Vextices--;
g->Arcs-=num;
}
T FindNeibro(MGraph *g,T e)
{
int i=0;
int p1=0;
p1=locate(g,e);
if (p1==-1)
{
return;
}
for (i=0;i<g->Vextices;i++)
{
if (g->p[p1][i]==1)
{
return g->vexlist[i];
}
}
}
T FindnextNeibro(MGraph *g,T v1,T v2)
{
int i=0;
int p1=locate(g,v1);
int p2=locate(g,v2);
if (p1==-1||p2==-1)
{
return;
}
for (i=p2+1;i<g->Vextices;i++)
{
if (g->p[p1][i]==1)
{
return g->vexlist[i];
}
}
return -1;
}
void DestroyMG(MGraph *g)
{
int i=0;
free(g->vexlist);
g->vexlist=NULL;
for (i=0;i<g->MaxVextices;i++)
{
free(g->p[i]);
}
free(g->p);
g->p=NULL;
g->MaxVextices=0;g->Arcs=0;g->Vextices=0;
}
//**************************************************//
//***邻接矩阵的主函数(操作函数)***//
#include "MGraph.h"
//void main()
//{
//
// MGraph M;
// InitMgraph(&M);
// InsertMG(&M,'a');
// InsertMG(&M,'b');
// InsertMG(&M,'c');
// InsertMG(&M,'d');
// Insertedge(&M,'a','b');
// Insertedge(&M,'c','b');
// Insertedge(&M,'a','d');
// Insertedge(&M,'d','c');
// showMG(&M);
// removeEdge(&M,'a','b');
// showMG(&M);
// removeVertice(&M,'a');
// showMG(&M);
// DestroyMG(&M);
// printf("%d\n",M.Arcs);
//
//}
void main()
{
T n1;
T n2;
MGraph M;
InitMgraph(&M);
InsertMG(&M,'a');
InsertMG(&M,'b');
InsertMG(&M,'c');
InsertMG(&M,'d');
Insertedge(&M,'a','b');
Insertedge(&M,'c','b');
Insertedge(&M,'a','d');
Insertedge(&M,'d','c');
showMG(&M);
n1= FindNeibro(&M,'c');
n2=FindnextNeibro(&M,'c',n1);
printf("%c %c",n1,n2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值