数据结构:无向图2

#include <iostream>


using namespace std;


int G[10][10]={ {0,1,0,1,0},
       {1,0,1,0,1},
               {0,1,0,1,1},
       {1,0,1,0,0},
       {0,1,1,0,0}, };


int vexnum=5;


void show()
{
for(int i=0;i<vexnum;i++)
{
for(int j=0;j<vexnum;j++)
cout<<G[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}


int arcnum() //计算图的边数
{
int n=0;
for(int i=0;i<vexnum;i++)
{
for(int j=0;j<i;j++)
if(G[i][j])
n++;
}
return n;



void addVex() //增加新顶点 
{
vexnum++;
}


void addArc(int v,int w)
{
int i=v-1;
int j=w-1;
G[i][j] = G[j][i] = 1;



int main()
{
show();
cout<<"arcnum="<<arcnum()<<endl;

addVex();
addArc(4,vexnum); //在新顶点和第4个顶点之间增加一条边
show();
cout<<"arcnum="<<arcnum()<<endl;

return 0;
}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80508751
今日推荐