数据结构(C语言版)规范代码之图(邻接矩阵与邻接表)

我只写了有相网和无向网的创建方法,以及与邻接表的转换方法,因为图的要简单一些,所以略去了,这个应付作业足够了~还希望大家多多指出不足


//图-邻接矩阵
#include<Iostream>
#include<Cstdlib>
#include<malloc.h>
using namespace std;
#define INFINITY INT_MAX//最大值
#define MAX_VERTEX_NUM 20//最大定点个数
#define VRType int//定点关系类型,一般是整数表示
#define InfoType char//弧相关信息的指针类型
#define VertexType char//定点向量类型
#define Status int//函数返回的状态
#define NONE 0
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
typedef enum {DG,DN,UDG,UDN} GraphKind;
typedef struct ArcCell//矩阵单元结构
{
 VRType adj;//定点关系,无权图用01,有权图则为权值类型
 InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct//图结构
{
 VertexType vexs[MAX_VERTEX_NUM];
 AdjMatrix arcs;//指向其邻接矩阵的的指针
 int vexnum,arcnum;//图中定点和弧数
 GraphKind kind;
}MGraph;
typedef struct ArcNode//邻接表的弧结构
{
 int adjvex;//该弧指向的顶点位置
 struct ArcNode *nextarc;//指向下一个弧
 InfoType *info;//该弧相关信息
 int Power;//该弧的权值
}ArcNode;
typedef struct VNode//顶点结构
{
 VertexType data;//顶点信息
 ArcNode *firstarc;//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct//临界表的结构
{
 AdjList vertices;
 int vexnum,arcnum;//顶点和弧的个数
 GraphKind kind;
}ALGraph;
Status CreateGraph(MGraph &G);//创建一个图,并返回给G
Status CreateUDN(MGraph &G);//创建一个无向网G
Status CreateDN(MGraph &G);//创建一个有向网G
Status PrintMatrix(AdjMatrix arcs,int vexnum);//输出一个邻接矩阵
Status MToAL(MGraph &G1,ALGraph &G2);//矩阵转化为临界表
Status PrintAL(AdjList vertices,int vexunm);//输出一个临界表
void main()//主函数
{
 MGraph MG;
 ALGraph AG;//先上道具
 CreateGraph(MG);//主角出场
 PrintMatrix(MG.arcs,MG.vexnum);//先看看矩阵的样子
 MToAL(MG,AG);//变身
 PrintAL(AG.vertices,AG.vexnum);//看下临界表
 //谢幕
}
Status CreateGraph(MGraph &G)
{
 int i=4;
 cout<<"What kind of graph shall we create?:1.DG 2.DN 3.UDG 4.UDN"<<endl;
 cin>>i;
 switch(i)
 {
 case 1:
  G.kind=DG;
  return NONE;//本程序不实现先
 case 2:
  G.kind=DN;
  return CreateDN(G);//本程序不实现先
 case 3:
  G.kind=UDG;
  return NONE;//本程序不实现先
 case 4:
  G.kind=UDN;
  return CreateUDN(G);//只实现这个无向网的
 default:
  return ERROR;
 }
}
Status CreateUDN(MGraph &G)//无向网
{
 int i,j,k;//定义循环变量
 int adj;//权值
 cout<<"Please input the vexnum and arcnum:";//输入定点和弧的个数
 cin>>G.vexnum>>G.arcnum;
 cout<<"Please input the vexs:";//输入定点向量vexnum个
 for(i=0;i<G.vexnum;i++)
  cin>>G.vexs[i];
 for(i=0;i<G.vexnum;i++)//初始化一下下
  for(j=0;j<G.vexnum;j++)
  {
   G.arcs[i][j].adj=INFINITY;
   G.arcs[i][j].info=NULL;
  }
 cout<<"Please input the location and the adj:"<<endl;//输入地址和相应权值
 for(k=0;k<G.arcnum;k++)
 {
  cin>>i>>j>>adj;
  G.arcs[i][j].adj=adj;
  //信息输入略
  G.arcs[j][i]=G.arcs[i][j];//对称矩阵
 }
 return OK;
}
Status CreateDN(MGraph &G)//有向网
{
 int i,j,k;//定义循环变量
 int adj;//权值
 cout<<"Please input the vexnum and arcnum:";//输入定点和弧的个数
 cin>>G.vexnum>>G.arcnum;
 cout<<"Please input the vexs:";//输入定点向量vexnum个
 for(i=0;i<G.vexnum;i++)
  cin>>G.vexs[i];
 for(i=0;i<G.vexnum;i++)//初始化一下下
  for(j=0;j<G.vexnum;j++)
  {
   G.arcs[i][j].adj=INFINITY;
   G.arcs[i][j].info=NULL;
  }
 cout<<"Please input the location and the adj:"<<endl;//输入地址和相应权值
 for(k=0;k<G.arcnum;k++)
 {
  cin>>i>>j>>adj;
  G.arcs[i][j].adj=adj;
  //信息输入略
 }
 return OK;
}
Status PrintMatrix(AdjMatrix arcs,int vexnum)//输出整个矩阵
{
 cout<<"Here is the Matrix:"<<endl;
 int i,j;
 for(i=0;i<vexnum;i++)
 {
  for(j=0;j<vexnum;j++)
  {
   if(arcs[i][j].adj==INFINITY)
    cout<<(char)2<<" ";
   else
    cout<<arcs[i][j].adj<<" ";
  }
  cout<<endl;
 }
 return OK;
}
Status MToAL(MGraph &G1,ALGraph &G2)//矩阵转化为邻接表

 ArcNode *Arcp=NULL;//定义一个弧节点的指针
 ArcNode *Tail=NULL;//弧链表的尾巴指针
 int i,j;
 G2.arcnum=G1.arcnum;
 G2.vexnum=G1.vexnum;
 G2.kind=G1.kind;
 for(i=0;i<G2.vexnum;i++)
  G2.vertices[i].firstarc=NULL;//将所有的节点的指针都初始化
 for(i=0;i<G2.vexnum;i++)
  G2.vertices[i].data=G1.vexs[i];//转化定点
 for(i=0;i<G1.vexnum;i++)
  for(j=0;j<G1.vexnum;j++)
  {
   if(G1.arcs[i][j].adj!=INFINITY)//说明存在弧于是我们就开始分配空间
   {
    Arcp=(ArcNode *)malloc(sizeof(ArcNode));//把分配的空间先给Arcp
    if(G2.vertices[i].firstarc==NULL)
    {
     G2.vertices[i].firstarc=Arcp;//如果是第一个就like this 你懂得~
     G2.vertices[i].firstarc->Power=G1.arcs[i][j].adj;//转化权值
     G2.vertices[i].firstarc->adjvex=j;
     Tail=G2.vertices[i].firstarc;//Tail指向了第一个弧节点
     Tail->nextarc=NULL;//初始化
    }
    else
    {
     Tail->nextarc=Arcp;//你懂得
     Tail->nextarc->Power=G1.arcs[i][j].adj;//转化权值哦~
     Tail->nextarc->adjvex=j;
     Tail=Tail->nextarc;//指向下一个节点
     Tail->nextarc=NULL;//初始化
    } 
   }
  }
 return OK;
}
Status PrintAL(AdjList vertices,int vexnum)//输出一个邻接表
{
 int i;
 ArcNode *p;
 for(i=0;i<vexnum;i++)
 {
  cout<<"The ver:"<<vertices[i].data<<" The arc:";
  p=vertices[i].firstarc;
  while(p!=NULL)//输出弧节点的权值
  {
   cout<<vertices[p->adjvex].data<<" ";
   p=p->nextarc;
  }
  cout<<endl;
 }
 return OK;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值