数据结构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);