问题 A: 将邻接矩阵存储的图转换为邻接表存储的图,附加代码模式

题目描述

用邻接表法存储的图如下所示:

要求将邻接矩阵存储的图,改造为邻接表存储的图,并输出。


本题为附加代码模式,以下代码为自动附加在同学们提交的代码后面。在本题的提示中有代码框架,请同学们拷贝后,修改,再注释掉部分代码,最后提交。

// 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.vexNumber;i++){
        g.info[i] = (char)('a'+i);
        for(int j=0;j<g.vexNumber;j++){
            cin >> 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 <iostream>
#include <cstdio>
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.vexNumber;i++){
        g.info[i] = (char)('a'+i);
        for(int j=0;j<g.vexNumber;j++){
            cin >> g.adjMatrix[i][j];
        }
    }
    linkGraph G;
    InitGraph(G,g);
    PrintGraph(G);
    DestroyGraph(G);
    return 0;
}

代码展示 

观察输出样例应用头插法,注意“ --> ” 前后是有空格的

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
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;i<g.vexNumber;i++){
		G.vexes[i].firstarc=NULL;
		G.vexes[i].Info=g.info[i];
	}
	return 0;
}
//将邻接矩阵存储的图转换为领接表存储的图
void InitGraph(linkGraph &G,const Graph &g){
	preInitGraph(G,g);
	for(int i=0;i<G.vexnumber;i++){
		for(int j=0;j<G.vexnumber;j++){
			if(g.adjMatrix[i][j]==1){
				ArcNode *p=new ArcNode();
				p->nextarc=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;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){
	for(int i=0;i<G.vexnumber;i++){
		cout<<G.vexes[i].Info;
		ArcNode *p=G.vexes[i].firstarc;
		while(p!=NULL){
			cout<<" --> "<<G.vexes[p->adj].Info;
			p=p->nextarc;
		}
		cout<<endl;
	}
}

//comment
/*
int main(){
	//freopen("/config/workspace/test/test","r",stdin);
	Graph g;
	cin>>g.vexNumber;
	for(int i=0;i<g.vexNumber;i++){
		g.info[i]=(char)('a'+i);
		for(int j=0;j<g.vexNumber;j++){
			cin>>g.adjMatrix[i][j];
		}
	}
	linkGraph G;
	InitGraph(G,g);
	PrintGraph(G);
	DestroyGraph(G);

	return 0;
}
*/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值