基本操作只包括了,结点的插入,边的插入、删除。(结点删除类似数组线性表的删除,需要分别把结点数组和边数组移动)
#include<iostream>
using namespace std;
#define Num 40
typedef char Vtype;
typedef int Etype;
typedef struct {
Vtype Vex[Num];
Etype Edge[Num][Num];
int vexnum=0;
int arcnum=0;
}MGraph;
bool GEmpty(MGraph G)
{
if(G.Vex[0]=='\0')
return true;
else
return false;
}
bool GFull(MGraph G)
{
if(G.Vex[Num]!=NULL)
return true;
else
return false;
}
bool Adjacent(MGraph G,Vtype x,Vtype y)
{
int a,b;
int i=0,j=0;
for(i;i<G.vexnum;i++)
{
for(j;j<G.vexnum;j++)
{
if(G.Vex[i]==x&&G.Vex[j])
return true;
}
}
return false;
}
void InsertV(MGraph &G,Vtype v)
{
int i=0;
int flag = 1;
if(!GFull(G)){
while(i<=G.vexnum)
{
if(G.Vex[i]==v){
cout<<"There has been exist this node."<<endl;
flag=0;
break;
}
i++;
}
if(flag==1)
{
G.Vex[G.vexnum] = v;
}
}
else
cout<<"Graph is full."<<endl;
}
void InsertE(MGraph &G,Vtype x,Vtype y)
{
int i=0,j=0;
int a,b;
int n;
n=G.vexnum;
while(n!=0)
{
if(G.Vex[i]==x)
a=i;
else
i++;
if(G.Vex[j]==y){
b=j;
}else
j++;
n--;
}
G.Edge[a][b]=1;
G.arcnum++;
}
void DeleteE(MGraph &G,Vtype x,Vtype y)
{
int i=0,j=0;
int a,b;
int n;
n=G.vexnum;
while(n!=0)
{
if(G.Vex[i]==x)
a=i;
else
i++;
if(G.Vex[j]==y){
b=j;
}else
j++;
n--;
}
G.Edge[a][b]=0;
G.arcnum--;
}
int main()
{
MGraph G;
Vtype v[6] = {'a','b','c','d','e','f'};
for(int i=0;i<6;i++)
{
InsertV(G,v[i]);
G.vexnum++;
}
char a,b;
int flag1=1,flag2=1;
do{
cout<<"input edge between v1,v2:"<<endl;
cin>>a;
cin>>b;
cout<<"check 0 "<<endl;
InsertE(G,a,b);
cout<<"check 1 "<<endl;
cout<<"input 1 to continue,0 to end"<<endl;
cin>>flag1;
}while(flag1==1);
char c,d;
cout<<"adjace : "<<endl;
cin>>c>>d;
if(Adjacent(G,c,d))
cout<<"there exist a edge"<<endl;
else
cout<<"there has no edge"<<endl;
return 0;
}
2839




被折叠的 条评论
为什么被折叠?



