题目描述
用邻接表法存储的图如下所示:
要求将邻接矩阵存储的图,改造为邻接表存储的图,并输出。
本题为附加代码模式,以下代码为自动附加在同学们提交的代码后面。在本题的提示中有代码框架,请同学们拷贝后,修改,再注释掉部分代码,最后提交。
// please comment the following code when you submit to OJ
int main(){
//freopen("/config/workspace/answer/test.in","r",stdin);
//freopen("/config/workspace/answer/test.out","w",stdout);
Graph g;
cin >> g.vexNumber;
for(int i=0;i> g.adjMatrix[i][j];
}
}
linkGraph G;
InitGraph(G,g);
PrintGraph(G);
DestroyGraph(G);
return 0;
}
输入格式
输入的第一行包含一个正整数n,表示图中共有n个顶点。其中n不超过50。 以后的n行中每行有n个用空格隔开的整数0或1,对于第i行的第j个0或1,1表示第i个顶点和第j个顶点有直接连接,0表示没有直接连接。当i和j相等的时候,保证对应的整数为0。 注意输入的邻接矩阵不一定为对称矩阵,即输入的图一定是可能是有向图,也可能是无向图。
输出格式
对每个节点,输出节点字母,及其所有的邻接点
输入样例
7
0 0 0 0 0 1 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 1
0 1 0 1 0 0 0
输出样例
a --> g --> f
b --> c
c --> e
d
e --> d
f --> g --> e
g --> d --> b
数据范围与提示
代码框架如下:
#include
#include
using namespace std;
#define MAX_SIZE 100
// 邻接矩阵存储的图
struct Graph{
int vexNumber;
string info[MAX_SIZE];
int adjMatrix[MAX_SIZE][MAX_SIZE];
};
// 弧结点定义
struct ArcNode
{
int weight; // 弧上的信息部分
int adj; // 邻接点的序号
ArcNode *nextarc;
};
// 顶点结点定义
struct VexNode
{
string Info; // 顶点上的信息部分
ArcNode *firstarc; // 弧链头指针
};
// 邻接表结构的图的定义
struct linkGraph
{
VexNode *vexes;
int vexnumber;
};
int InitGraph(linkGraph &G, int vexnumber)
{
G.vexes = new VexNode[vexnumber];
G.vexnumber = vexnumber;
for (int i = 0; i < vexnumber; i++)
G.vexes[i].firstarc = NULL;
return 0;
}
// 将邻接矩阵存储的图转换为邻接表存储的图
void InitGraph(linkGraph &G, const Graph& g){
}
int DestroyGraph(linkGraph &G)
{
for (int i = 0; i < G.vexnumber; i++)
{
while (G.vexes[i].firstarc != NULL)
{
ArcNode *p = G.vexes[i].firstarc;
G.vexes[i].firstarc = p->nextarc;
delete p;
}
}
delete[]G.vexes;
G.vexes = NULL;
G.vexnumber = 0;
return 0;
}
// 输出邻接表存储的图
void PrintGraph(const linkGraph& G){
}
// please comment the following code when you submit to OJ
int main(){
//freopen("/config/workspace/answer/test.in","r",stdin);
//freopen("/config/workspace/answer/test.out","w",stdout);
Graph g;
cin >> g.vexNumber;
for(int i=0;i> g.adjMatrix[i][j];
}
}
linkGraph G;
InitGraph(G,g);
PrintGraph(G);
DestroyGraph(G);
return 0;
}
代码展示
观察输出样例应用头插法,注意“ --> ” 前后是有空格的
#include
#include
#include
#include
using namespace std;
#define MAX_SIZE 100
//领接矩阵存储的图
struct Graph{
int vexNumber;
string info[MAX_SIZE];
int adjMatrix[MAX_SIZE][MAX_SIZE];
};
//弧结点定义
struct ArcNode{
int weight;//弧上的信息部分
int adj;//邻接点的序号
ArcNode *nextarc;
};
//顶点结点定义
struct VexNode{
string Info;
ArcNode *firstarc;
};
//领接表结构的图的定义
struct linkGraph{
VexNode *vexes;
int vexnumber;
};
int preInitGraph(linkGraph &G,const Graph &g){
G.vexes=new VexNode[g.vexNumber];
G.vexnumber=g.vexNumber;
for(int i=0;inextarc=NULL;
p->adj=j;
if(G.vexes[i].firstarc==NULL){
G.vexes[i].firstarc=p;
}
else{
ArcNode *q=G.vexes[i].firstarc;
G.vexes[i].firstarc=p;
p->nextarc=q;//头插
}
}
}
}
}
int DestroyGraph(linkGraph &G){
for(int i=0;inextarc;
delete p;
}
}
delete[]G.vexes;
G.vexes=NULL;
G.vexnumber=0;
return 0;
}
//输出领接表存储的图
void PrintGraph(const linkGraph &G){
for(int i=0;i "<adj].Info;
p=p->nextarc;
}
cout<>g.vexNumber;
for(int i=0;i>g.adjMatrix[i][j];
}
}
linkGraph G;
InitGraph(G,g);
PrintGraph(G);
DestroyGraph(G);
return 0;
}
*/