C語言實(shí)現(xiàn)圖的鄰接矩陣存儲(chǔ)操作
利用鄰接矩陣容易判定任意兩個(gè)頂點(diǎn)之間是否有邊(或?。┫噙B,并容易求得各個(gè)頂點(diǎn)的度。
c語言代碼實(shí)現(xiàn)如下:
#include<stdio.h>
#include<stdlib.h>
#define MAX_VER_NUM 50
typedef char VertexType;
typedef enum
{
DG,UDG
}GraphType;
typedef struct
{
VertexType vexs[MAX_VER_NUM]; //頂點(diǎn)向量
int arcs[MAX_VER_NUM][MAX_VER_NUM]; //鄰接矩陣
int vexnum,arcnum; //圖的當(dāng)前頂點(diǎn)數(shù)和弧數(shù)
GraphType type; //圖的種類標(biāo)志
}MGraph;
//根據(jù)名稱得到指定頂點(diǎn)在頂點(diǎn)集合中的下標(biāo)
//vex 頂點(diǎn)
//return 如果找到,則返回下標(biāo),否則,返回0
int getIndexOfVexs(char vex,MGraph *MG)
{
int i;
for(i=1;i<=MG->vexnum;i++)
{
if(MG->vexs[i]==vex)
{
return i;
}
}
return 0;
}
//創(chuàng)建鄰接矩陣
void create_MG(MGraph *MG)
{
int i,j,k;
int v1,v2,type;
char c1,c2;
printf("Please input graph type DG(0) or UDG(1):");
scanf("%d",&type);
if(type==0)
{
MG->type=DG;
}
else if(type==1)
{
MG->type=UDG;
}
else
{
printf("Please input correct graph type DG(0) or UDG(1)!");
return;
}
printf("Please input vexnum:");
scanf("%d",&MG->vexnum);
printf("Please input arcnum:");
scanf("%d",&MG->arcnum);
getchar();
for(i=1;i<=MG->vexnum;i++)
{
printf("Please input %dth vex(char):",i);
scanf("%c",&MG->vexs[i]);
getchar();
}
//初始化鄰接矩陣
for(i=1;i<=MG->vexnum;i++)
{
for (j=1;j<=MG->vexnum;j++)
{
MG->arcs[i][j]=0;
}
}
//輸入邊的信息,建立鄰接矩陣
for(k=1;k<=MG->arcnum;k++)
{
printf("Please input %dth arc v1(char) v2(char):",k);
scanf("%c %c",&c1,&c2);
v1=getIndexOfVexs(c1,MG);
v2=getIndexOfVexs(c2,MG);
if(MG->type==-1)
{
MG->arcs[v1][v2]=MG->arcs[v2][v1]=1;
}
else
{
MG->arcs[v1][v2]=1;
}
getchar();
}
}
//打印鄰接矩陣和頂點(diǎn)信息
void print_MG(MGraph MG)
{
int i,j;
if(MG.type==DG)
{
printf("Graph type: Direct graph\n");
}
else
{
printf("Graph type: Undirect graph\n");
}
printf("Graph vertex number: %d\n",MG.vexnum);
printf("Graph arc number: %d\n",MG.arcnum);
printf("Vertex set:");
for(i=1;i<=MG.vexnum;i++)
{
printf("%c",MG.vexs[i]);
}
printf("\nAdjacency Matrix:\n");
for(i=1;i<=MG.vexnum;i++)
{
for(j=1;j<=MG.vexnum;j++)
{
printf("%d",MG.arcs[i][j]);
}
printf("\n");
}
}
//主函數(shù)
int main(void)
{
MGraph MG;
create_MG(&MG);
print_MG(MG);
return 0;
}
得到的結(jié)果如下圖所示:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中操作sqlserver數(shù)據(jù)庫案例教程
這篇文章主要介紹了C語言中操作sqlserver數(shù)據(jù)庫案例教程,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
使用C語言遞歸與非遞歸實(shí)現(xiàn)字符串反轉(zhuǎn)函數(shù)char *reverse(char *str)的方法
本篇文章是對(duì)使用C語言遞歸與非遞歸實(shí)現(xiàn)字符串反轉(zhuǎn)函數(shù)char *reverse(char *str)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語言詳細(xì)講解strcpy strcat strcmp函數(shù)的模擬實(shí)現(xiàn)
這篇文章主要介紹了怎樣用C語言模擬實(shí)現(xiàn)strcpy與strcat和strcmp函數(shù),strcpy()函數(shù)是C語言中的一個(gè)復(fù)制字符串的庫函數(shù),strcat()函數(shù)的功能是實(shí)現(xiàn)字符串的拼接,strcmp()函數(shù)作用是比較字符串str1和str2是否相同2022-05-05
C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒有參數(shù),通過清晰的實(shí)例源代碼,詳細(xì)解釋了它們?cè)诤瘮?shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01
數(shù)組中求第K大數(shù)的實(shí)現(xiàn)方法
本篇文章是對(duì)數(shù)組中求第K大數(shù)的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語言 TerminateProcess函數(shù)案例詳解
這篇文章主要介紹了C語言 TerminateProcess函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C/C++: Inline function, calloc 對(duì)比 malloc
以下是對(duì)c/c++中的malloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進(jìn)行了介紹,需要的朋友可以過來參考下2016-07-07

