邻接矩阵存储结构上实现图的基本操作
作者:互联网
//uestc 的吧,多用心写代码,别只顾着copy
bool matrix_insert_vertex(MatrixGraph *G, VertexType v){
if(matrix_locate_vertex(G,v) != -1){
return false;
}
if(G -> vexnum == MAX_VERTEX_NUM){
return false;
}
G -> vertex[G->vexnum] = v;
for (int i = 0; i <= G->vexnum; i++) {
G->arcs[i][G -> vexnum] = 0;
G->arcs[G -> vexnum][i] = 0;
}
G->vexnum++;
return true;
}
bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w){
if(matrix_locate_vertex(G,v) == -1 || matrix_locate_vertex(G,w) == -1){
return false;
}
int i = matrix_locate_vertex(G,v);
int j = matrix_locate_vertex(G,w);
if(G -> type == UDG){
if(G->arcs[i][j] == 1 || G->arcs[j][i] == 1){
return false;
}
G->arcs[i][j] = 1;
G->arcs[j][i] = 1;
}
if(G -> type == DG){
if(G->arcs[i][j] == 1 ){
return false;
}
G->arcs[i][j] = 1;
}
G -> arcnum++;
return true;
}
// created by ttj
标签:vexnum,存储,return,matrix,arcs,vertex,邻接矩阵,基本操作,false 来源: https://blog.csdn.net/m0_51430625/article/details/117265618