图的邻接表和邻接矩阵的相互转换

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
MG=new MGraph;
MG->e=0;
MG->n=5;
int j;
for(int i=0;i<MAXV;i++)
MG->vexs[i].no=i;
for(i=0;i<MAXV;i++)
for(j=0;j<MAXV;j++)
MG->edges[i][j]=0;
}
void InitialALG(ALGraph *&ALG)
{
ALG=new ALGraph;
ALG->n=5;
ALG->e=0;
for(int i=0;i<MAXV;i++)
{
ALG->adjlist[i].data=i;
ALG->adjlist[i].firstarc=NULL;
}
}
void InputALG(ALGraph *&ALG)
{
int ch;
ArcNode *p;
cout<<endl<<"请输入邻接表,以-1结束输入(形式如:4:5 5 6 -1):"<<endl;
for(int i=0;i<ALG->n;i++)
{
cout<<ALG->adjlist[i].data<<" : ";
cin>>ch;
if(ch!=-1)
{
ALG->adjlist[i].firstarc=new ArcNode;
{
int i;
ArcNode *p;
for(i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
g.edges[i][p->adjvex]=1;
p=p->nextarc;
}
}
g.n=G->n;
g.e=G->e;
}
void main()
{
// MGraph *MG;
cout<<endl;
}
cout<<endl;
}
void Transchange(MGraph *&MG,ALGraph *&ALG)
{
int i,j;
ArcNode *p;
ALG->n=MG->n;
ALG->e=MG->e;
for(i=0;i<MG->n;i++)
{
for(j=0;j<MG->n;j++)
{
int i,j;
ArcNode *p;
MG->n=ALG->n;
for(i=0;i<ALG->n;i++)
{
if(ALG->adjlist[i].firstarc!=NULL)
{
p=ALG->adjlist[i].firstarc;
while(p!=NULL)
{
MG->edges[i][p->adjvex]=1;
{
if(MG->edges[i][j]!=0)
{
p=new ArcNode;
p->adjvex=j;
p->nextarc=ALG->adjlist[i].firstarc;
ALG->adjlist[i].firstarc=p;
}
}
}
}
void ALGtoMG(ALGraph *&ALG,MGraph *MG)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=g.n;
G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g) //将邻接表G转换为邻接矩阵g
int j=0;
for(int i=0;i<MG->n;i++)
for(j=0;j<MG->n;j++)
cin>>MG->edges[i][j];
for(i=0;i<MG->n;i++)
for(j=0;j<MG->n;j++)
if(MG->edges[i][j]==1)
{
MG->e++;
}
iቤተ መጻሕፍቲ ባይዱ(H=='N')
}
}
}
void OutputALG(ALGraph *&ALG)
{
ArcNode *p;
cout<<"邻接表如下:"<<endl;
for(int i=0;i<ALG->n;i++)
{
cout<<ALG->adjlist[i].data<<" : ";
p=ALG->adjlist[i].firstarc;
while(p!=NULL)
{
cout<<p->adjvex<<"\t";
p=p->nextarc;
}
cout<<endl;
}
}
void InputMG(MGraph *&MG)
{
char H;
cout<<"请输入(H/N)(有向/无向):"<<endl;
cin>>H;
cout<<"请以矩阵的方式输入顶点关系:\n";
typedef struct
{
int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
}MGraph;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
InfoType info;
}ArcNode;
{
int i,j;
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for(i=0;i<g.n;i++)
G->adjlist[i].firstarc=NULL;
for(i=0;i<g.n;i++)
for(j=g.n-1;j>=0;j--)
if(g.edges[i][j]!=0)
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
typedef int InfoType;
typedef int Vertex;
#define MAXV 5
typedef struct
{
int no;
InfoType info;
}VertexType;
{
MG->e=MG->e/2;
}
}
void OutputMG(MGraph *&MG)
{
int j;
cout<<endl<<"图的邻接矩阵如下:"<<endl;
for(int i=0;i<MG->n;i++)
{
for(j=0;j<MG->n;j++)
cout<<MG->edges[i][j]<<"\t";
p=p->nextarc;
}
}
}
for(i=0;i<MG->n;i++)
{
for(j=0;j<MG->n;j++)
{
if(MG->edges[i][j]!=1)
{
MG->edges[i][j]=0;
}
}
}
}
void MatToList(MGraph g,ALGraph *&G) //将临街矩阵g转换成邻接表G
MGraph g,*p;
g.n=MAXV;
p=&g;
//InputMG(p);
//OutputMG(p);
//MatToList(g,ALG);
InitialMG(p);
ListToMat(ALG,*p);
OutputMG(p);
}
ALGraph *ALG;
InitialALG(ALG);
//InitialMG(MG);
//InputMG(MG);
//OutputMG(MG);
InputALG(ALG);
OutputALG(ALG);
//Transchange(MG,ALG);
//OutputALG(ALG);
//ALGtoMG(ALG,MG);
p=ALG->adjlist[i].firstarc;
p->adjvex=ch;
p->nextarc=NULL;
cin>>ch;
}
while(ch!=-1)
{
p->nextarc=new ArcNode;
p=p->nextarc;
p->adjvex=ch;
p->nextarc=NULL;
cin>>ch;
typedef struct Vnode
{
Vertex data;
ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MAXV];
typedef struct
{
AdjList adjlist;
int n,e;
}ALGraph;
void InitialMG(MGraph *&MG)
相关文档
最新文档