C语言 图 代码实现

#include<stdio.h>

//#include<string.h>

//#include<math.h>

#include<stdlib.h>

//函数结果状态代码

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define INFEASIBLE -1

#define OVERFLOW -2

#define NULL 0

#define MAXSIZE 100

#define MVNum 100//最大顶点数

#define ManInt 32767 //表示极大值,即∞

//Status是函数的类型,其值是函数结果状态代码

typedef int Status;

typedef char VerTexType;//设置顶点的数据类型

typedef int ArcType;//假设边的权值类型为int

//声明结点的类型和指向结点的指针类型

//邻接矩阵

typedef struct{

VerTexType vexs[MVNum];//顶点表

ArcType arcs[MVNum][MVNum];//邻接矩阵

int vexnum,arcnum;//图的当前点数和边数

}AMGraph;

//邻接表

typedef struct VNode{

VerTexType data;//顶点信息

ArcNode *firstarc;//指向第一条依附该顶点的边的指针

}VNode,AdjList[MVNum];//AdjList表示邻接表类型

//弧(边)的结点结构

typedef struct ArcNode{

int adjvex;//该边所指向的顶点的位置

struct ArcNode *nextarc;//指向下一条边的指针

OtherInfo info;//和边相关的信息

}ArcNode;

//图的结构定义

typedef struct{

AdjList vertices;//邻接表

int vexnum,arcnum;//图的当前顶点数和弧数

}ALGraph;

//邻接矩阵--在图中查找顶点

int LocateVex(AMGraph G,VerTexType u){

for(int i=0;i<G.vexnum;i++){

if(u==G.vexs[i]) return i;//查找成功,返回顶点u在顶点表中的下标

}

return -1;//查找失败,返回-1

}

//邻接矩阵--创建无向网

Status CreateUND(AMGraph &G){

scanf("%d %d",&G.vexnum,&G.arcnum);//输入总顶点数、总边数

for(int i=0;i<G.vexnum;i++){

scanf("%c",&G.vexs[i]);//输入顶点表的信息

}

//初始化邻接矩阵

for(int i=0;i<G.vexnum;i++){

for(int j=0;j<G.vexnum;j++){

G.arcs[i][j]=ManInt;

}

}

//构造邻接矩阵

for(int k=0;k<G.arcnum;k++){

VerTexType v1,v2;

ArcType w;

scanf("%d %d %d",&v1,&v2,&w);//输入一条边所依附的顶点及边的权值

//确定v1、v2在G中的位置

int i=LocateVex(G,v1);

int j=LocateVex(G,v2);

G.arcs[i][j]=w;//边<v1,v2>的权值置为w

G.arcs[j][i]=G.arcs[i][j];//置边<v1,v2>的对称边<v2,v1>的权值为w

}

return OK;

}

//邻接表--创建无向网

Status CreateUDG(ALGraph &G){

scanf("%d %d",G.vexnum,G.arcnum),//输入总顶点数、总边数

//构造表头结点表

for(int i=0;i<G.vexnum;i++){

scanf("%d",G.vertices[i].data);//输入顶点值

G.vertices[i].firstarc=NULL;//初始化表头结点的指针域

}

//构造邻接表

for(int k=0;k<G.arcnum;k++){

VerTexType v1,v2;

scanf("%c %c",&v1,&v2);//输入一条边依附的两个顶点

i=LocateVex(G,v1);

j=LocateVex(G,v2);

p1=new ArcNode;//生成一个新的边结点*p1

p1->adjvex=j;//邻接点序号为j

p1->nextarc=G.vertices[i].firstarc;//头插法插入

G.vertices[i].firstarc=p1;//将新结点*p1插入顶点vi的边表头部

p2=new ArcNode;

p2->adjvex=i;//邻接点序号为i

p2->nextarc=G.vertices[j].firstarc;//头插法插入

G.vertices[j].firstarc=p2;//将新结点*p2插入顶点vj的边表头部

}

return OK;

}

//深度优先搜索遍历--邻接矩阵

void DFS(AMGraph G,int v){

//访问第v个顶点

printf("%d",v);

visited[v]=true;

for(int w=0;i<G.vexnum;w++){

if(G.arcs[v][w]!=0 && !visited[w]){

DFS(G,w);//w是v的邻接点,如果w未被访问,则递归调用DFS

}

}

}

//广度优先搜索--非递归遍历

void BFS(Graph G,int v){

//访问第v个顶点

printf("%d",v);

visited[v]=true;

InitQueue(Q);//辅助队列Q初始化,置空

EnQueue(Q,v);//v入队

while(!QueueEmpty(Q)){//队列非空

DeQueue(Q,u);//队头元素出队并置为u

for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w)){//FirstAdjVex:u的第一条弧;NextAdjVex:找u的下一条弧并赋值给w

if(!visited[w]){//w为u的尚未访问的邻接顶点

//访问第w个顶点

printf("%d",w);

visited[w]=true;

EnQueue(Q,w);//w入队

}

}

}

}

int main(){

return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值