|
|
#2
寒风中的细雨2010-12-24 12:39
程序代码:#include <iostream> #include <cstdlib> using namespace std; #define MAXV 100 //<最大顶点个数>// typedef struct { int no; /*顶点编号*/ char data; /*顶点其他信息*/ }VertexType; /*顶点类型*/ typedef struct /*图的定义*/ { int edges[MAXV][MAXV]; /*邻接矩阵*/ int vexnum,arcnum; /*顶点数,弧数*/ VertexType vexs[MAXV]; /*存放顶点信息*/ }MGraph; void creatMGraph(MGraph **G) { // VertexType *V; *G=(MGraph *)malloc(sizeof(MGraph)); // V=(VertexType *)malloc(sizeof(VertexType)); cout<<"输入图的顶点数:"<<endl; // int n; // cin >> n; cin >> (*G)->vexnum; for( int m=1; m<=(*G)->vexnum; ++m ) { cout<<"请输入图的顶点信息:"<<endl; cin >> (*G)->vexs[m].data; (*G)->vexs[m].no = m; } /*for(int m=1;m<=n;m++) { cout<<"请输入图的顶点信息:"<<endl; cin>>V->data; V->no=m; }*/ cout<<"输入图的边数:"<<endl; cin>>(*G)->arcnum; cout<<"如果边v1-》v2,则输入1,2"<<endl; // for(int i=0;i<n;i++) for(int k=0; k<=(*G)->arcnum; k++) { for(int j=0;j<=(*G)->arcnum;j++) (*G)->edges[k][j]=0; } for(int i=1;i<=(*G)->arcnum;i++) { cout<<"请输入第"<<i<<"边:"<<endl; int n1,n2; cin>>n1; cin>>n2; (*G)->edges[n1][n2]=1; (*G)->edges[n2][n1]=1; } } void disMGraph(MGraph *G) { cout<<endl<<"图的邻接矩阵为:"<<endl; for(int i=1;i<=G->vexnum;i++) { for(int j=1;j<=G->vexnum;j++) cout<<G->edges[i][j]; cout << endl; } } //#include "pubuse.h" //#include "GraphAlgo.h" //#include "GraphDef.h" void main() { MGraph *G=NULL; creatMGraph(&G); disMGraph(G); } //数据结构菜鸟求解各位高手这个程序哪里错了 只有本站会员才能查看附件,请 登录 |
#define MAXV 100 //<最大顶点个数>//
typedef struct
{ int no; /*顶点编号*/
char data; /*顶点其他信息*/
}VertexType; /*顶点类型*/
typedef struct /*图的定义*/
{ int edges[MAXV][MAXV]; /*邻接矩阵*/
int vexnum,arcnum; /*顶点数,弧数*/
VertexType vexs[MAXV]; /*存放顶点信息*/
}MGraph;
void creatMGraph(MGraph *G)
{
VertexType *V;
G=(MGraph *)malloc(sizeof(MGraph));
V=(VertexType *)malloc(sizeof(VertexType));
cout<<"输入图的顶点数:"<<endl;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"请输入图的顶点信息:"<<endl;
cin>>V->data;
V->no=i;
}
cout<<"输入图的边数:"<<endl;
cin>>G->arcnum;
cout<<"如果边v1-》v2,则输入1,2"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
G->edges[i][j]=0;
}
for(int i=1;i<=G->arcnum;i++)
{ cout<<"请输入第"<<i<<"边:"<<endl;
int n1,n2;
cin>>n1;
cin>>n2;
G->edges[n1][n2]=1;
G->edges[n2][n1]=1;
}
}
void disMGraph(MGraph *G)
{
cout<<endl<<"图的邻接矩阵为:"<<endl;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<G->edges[i][j];
}
#include "pubuse.h"
#include "GraphAlgo.h"
#include "GraphDef.h"
#include <iostream>
using namespace std;
void main()
{
MGraph *G;
creatMGraph(G);
disMGraph(G);
}
数据结构菜鸟求解各位高手这个程序哪里错了
程序代码: