你是要把邻接表转换成邻接矩阵还是要一个邻接矩阵表示的代码啊?好像不能直接转吧#pragma once#define MAX_VERTEX_NUM 20typedef enum{DG,DN,UDG,UDN}GraphKind;#define INFINTY 1000#include using namespace std;class ArcCell{ friend class MGraph;public: int adj; int *info;};class Closedge{ friend class MGraph;public: int adjvex; int lowcost;};class MGraph{public: char vexs[MAX_VERTEX_NUM]; ArcCell arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; int vexnum,arcnum; GraphKind kind; bool Visited[MAX_VERTEX_NUM]; //bool Visted[MAX_VERTEX_NUM]; //遍历的访问数组 Closedge closedge[MAX_VERTEX_NUM];public: MGraph(void); ~MGraph(void); int LocateVex(char ch) { for(int i=0;ivexnum;i++) { if(this->vexs[i]==ch) return i; } return 0; } void CreateUND() { int vexn,arcn,i,j; cout<<"请输入节点个数:"; cin>>vexn; cout<<"请输入弧数:"; cin>>arcn; this->arcnum=arcn; this->vexnum=vexn; for(i=0;i>this->vexs[i]; } for(i=0;iarcs[i][j].info=NULL; } cout<<"请输入弧的节点及其权值:"<>a; cout<<"请输入节点2:"; cin>>b; cout<<"请输入权值大小:"; cin>>w; x=this->LocateVex(a); y=this->LocateVex(b); this->arcs[x][y].adj=w; this->arcs[y][x].adj=this->arcs[x][y].adj; }}/* void GraphPrint() { for(int i=0;iLocateVex(ch); for(int j=0;jvexnum;++j) { if(j!=k) { this->closedge[j].adjvex=ch; this->closedge[j].lowcost=this->arcs[k][j].adj; } } this->closedge[k].lowcost=0; for(int i=1;ivexnum;++i) { //k=this->minimum(); int min=1000; for(int j=0;j0 && closedge[j].lowcostvexs[k]<vexnum;j++) { if(this->arcs[k][j].adjvexs[k]; closedge[j].lowcost=this->arcs[k][j].adj; } } } } int minimum() { Closedge temp; ; for(int i=0;ivexnum;++i) { if(>closedge[i].lowcost) { [i].lowcost; } } return ; } */ void BFSTraverse() { int v,r=0,f=0; int w; int cq[20]; for(v=0;vvexnum;v++) { this->Visited[v]=0; } for(v=0;vvexnum;v++) { cq[v]=-1; } for(v=0;vvexnum;v++) { if(!this->Visited[v]) { this->Visited[v]=1; cout<vexs[v]<<"\t"; cq[r]=v; while(cq[f]!=-1) { v=cq[f]; f++; // int i; for(w=this->FirstAdjVex(v);w!=-1;w=this->NextAdjVex(v,w)) { /*for(i=0;ivexnum;i++) if(this->vexs[i]==w) break;*/ if(!this->Visited[w]) { /*for(i=0;ivexnum;i++) if(this->vexs[i]==w) break;*/ this->Visited[w]=1; cout<vexs[w]<<"\t"; r++; cq[r]=w; } } } } } } int FirstAdjVex(int u) { int i,j; for(i=0;ivexnum;i++) { if(this->vexs[u]==this->vexs[i]) break; } //for(;ivexnum;i++) for(j=0;jvexnum;j++) { if(this->arcs[i][j].adj!=INFINTY) return j;//!!!!!!! } return -1; } int NextAdjVex(int u,int w) { int i,j; for(i=0;ivexnum;i++) { if(this->vexs[u]==this->vexs[i]) break; } //for(;ivexnum;i++) /*for(j=0;jvexnum;j++) if(j==w) break*/; for(j=w;jvexnum;j++) { if(this->arcs[i][j].adj!=INFINTY&&j!=w) return j; } return -1; } void DFS(int v) { this->Visited[v]=1; cout<vexs[v]<<"\t"; int w; for(w=this->FirstAdjVex(v);w!=-1;w=this->NextAdjVex(v,w)) { /*int i; for(i=0;ivexnum;i++) if(this->vexs[i]==w) break;*/ if(!this->Visited[w]) this->DFS(w); } } void DFSTraverse() { int v; for(v=0;vvexnum;v++) this->Visited[v]=0; for(v=0;vvexnum;v++) { if(!this->Visited[v]) this->DFS(v); } }};这个是我用c++写的一个邻接矩阵没粘main函数 不知是否符合你的要求