数据结构-图-邻接矩阵-试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc-icoding
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                数据结构-图-邻接矩阵-试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc-icoding
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                鄰接矩陣
試在鄰接矩陣存儲結構上實現圖的基本操作 matrix_insert_vertex 和matrix_insert_arc,相關定義如下:
typedef int VertexType;typedef enum{DG, UDG }GraphType;typedef struct{VertexType vertex[MAX_VERTEX_NUM]; //頂點向量int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //鄰接矩陣int vexnum, arcnum; //圖的當前頂點數和弧數GraphType type; //圖的種類標志 }MatrixGraph;int matrix_locate_vertex(MatrixGraph *MG, VertexType vex); //返回頂點 v 在vertex數組中的下標,如果v不存在,返回-1 bool matrix_insert_vertex(MatrixGraph *G, VertexType v); bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w);注意注意!!!
當成功插入頂點或邊時,函數返回true,否則(如頂點或邊已存在、插入邊時頂點v或w不存在)返回false。
參考代碼如下:
#include <stdio.h> #include "graph.h" // 請不要刪除,否則檢查不通過bool matrix_insert_vertex(MatrixGraph *G, VertexType v){int i;i = matrix_locate_vertex(G, v);if(i == -1 && G->vexnum < MAX_VERTEX_NUM - 1){G->vertex[G->vexnum++] = v;for(i = 0; i < G->vexnum; i++){G->arcs[i][G->vexnum -1] = 0;G->arcs[G->vexnum-1][i] = 0;}return true;}return false; }bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w){int i, j;i = matrix_locate_vertex(G, v);j = matrix_locate_vertex(G, w);if(i == -1 || j == -1) return false;if(G->type == UDG &&( G->arcs[i][j] == 1 || G->arcs[j][i] == 1))return false;else if(G->arcs[i][j] == 1) return false;if(G->type == UDG)G->arcs[j][i] = 1;G->arcs[i][j] = 1;G->arcnum++;return true; }?
總結
以上是生活随笔為你收集整理的数据结构-图-邻接矩阵-试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc-icoding的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 一般将来时的句子
- 下一篇: 邻接表1 - 试在邻接表存储结构上实现图
