没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int visited[10];/*访问标志数组*/
typedef struct ArcCell{
int adj;/*顶点关系类型,用1表示相邻,0表示不相邻*/
}ArcCell,**AdjMatrix;/*邻接矩阵*/
typedef struct type{
char data[3];/*顶点值*/
struct type *next;/*顶点的下一个指针*/
}VertexType;
typedef struct{
VertexType *vexs;/*顶点向量*/
AdjMatrix arcs;/*邻接矩阵*/
int vexnum,arcnum;/*图的顶点数和边数*/
}MGraph;
typedef struct CSNode{
char elem[3];
struct CSNode *lchild,*nextsibling;
}CSNode,*CSTree;
void InitGraph(MGraph *G)/*初始图*/
{ int i,nu,mu;
printf("\n输入顶点的个数和(边)弧的个数:");
scanf("%d%d",&nu,&mu);
G->arcs=(ArcCell **)malloc(nu*sizeof(ArcCell *));
for(i=0;i<nu;i++)/*分配邻接矩阵空间*/
G->arcs[i]=(ArcCell *)malloc(nu*sizeof(ArcCell));
G->vexs=(VertexType *)malloc(nu*sizeof(VertexType));/*分配顶点空间*/
G->vexnum=nu;G->arcnum=mu;/*图的顶点数和边数*/
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int visited[10];/*访问标志数组*/
typedef struct ArcCell{
int adj;/*顶点关系类型,用1表示相邻,0表示不相邻*/
}ArcCell,**AdjMatrix;/*邻接矩阵*/
typedef struct type{
char data[3];/*顶点值*/
struct type *next;/*顶点的下一个指针*/
}VertexType;
typedef struct{
VertexType *vexs;/*顶点向量*/
AdjMatrix arcs;/*邻接矩阵*/
int vexnum,arcnum;/*图的顶点数和边数*/
}MGraph;
typedef struct CSNode{
char elem[3];
struct CSNode *lchild,*nextsibling;
}CSNode,*CSTree;
void InitGraph(MGraph *G)/*初始图*/
{ int i,nu,mu;
printf("\n输入顶点的个数和(边)弧的个数:");
scanf("%d%d",&nu,&mu);
G->arcs=(ArcCell **)malloc(nu*sizeof(ArcCell *));
for(i=0;i<nu;i++)/*分配邻接矩阵空间*/
G->arcs[i]=(ArcCell *)malloc(nu*sizeof(ArcCell));
G->vexs=(VertexType *)malloc(nu*sizeof(VertexType));/*分配顶点空间*/
G->vexnum=nu;G->arcnum=mu;/*图的顶点数和边数*/
}
void InsertGraph(MGraph *G,int i,VertexType e)
{ if(i<0||i>G->vexnum) return;
G->vexs[i].next=e.next;
strcpy(G->vexs[i].data,e.data);
}
int Locate(MGraph G,VertexType v1)/*确定v1在图顶点中的位置*/
{ int i;
for(i=0;i<G.vexnum;i++)
if(strcmp(v1.data,G.vexs[i].data)==0) return i;
return -1;
}
void CreateUND(MGraph *G)/*采用数组(邻接矩阵)和邻接表表示无向图*/
{int i,j,k,p[10],d;
VertexType v1,v2,*q1,*q2,*q;
for(i=0;i<10;i++) p[i]=0;
for(i=0;i<G->vexnum;++i)/*初始邻接表*/
{ for(j=0;j<G->vexnum;++j) G->arcs[i][j].adj=0;}
for(k=0;k<G->arcnum;++k)
{printf("\n输入第 %d 条(边)弧相对的两个顶点值:\n",k+1);
d=0;
scanf("%s%s",v1.data,v2.data);/*输入相邻的两个点值*/
i=Locate(*G,v1);j=Locate(*G,v2);/*用i 和j来确定它们的位置*/
G->arcs[i][j].adj=1;
G->arcs[j][i].adj=G->arcs[i][j].adj;
q1=(VertexType *)malloc(sizeof(VertexType));/*为q1和q2各分配空间*/
q2=(VertexType *)malloc(sizeof(VertexType));/*不能是q1=q2=malloc这样只分配一个空间*/
strcpy(q1->data,G->vexs[i].data);
strcpy(q2->data,G->vexs[j].data);
q1->next=q2->next=NULL;/*以便连接在每个顶点后*/
void InsertGraph(MGraph *G,int i,VertexType e)
{ if(i<0||i>G->vexnum) return;
G->vexs[i].next=e.next;
strcpy(G->vexs[i].data,e.data);
}
int Locate(MGraph G,VertexType v1)/*确定v1在图顶点中的位置*/
{ int i;
for(i=0;i<G.vexnum;i++)
if(strcmp(v1.data,G.vexs[i].data)==0) return i;
return -1;
}
void CreateUND(MGraph *G)/*采用数组(邻接矩阵)和邻接表表示无向图*/
{int i,j,k,p[10],d;
VertexType v1,v2,*q1,*q2,*q;
for(i=0;i<10;i++) p[i]=0;
for(i=0;i<G->vexnum;++i)/*初始邻接表*/
{ for(j=0;j<G->vexnum;++j) G->arcs[i][j].adj=0;}
for(k=0;k<G->arcnum;++k)
{printf("\n输入第 %d 条(边)弧相对的两个顶点值:\n",k+1);
d=0;
scanf("%s%s",v1.data,v2.data);/*输入相邻的两个点值*/
i=Locate(*G,v1);j=Locate(*G,v2);/*用i 和j来确定它们的位置*/
G->arcs[i][j].adj=1;
G->arcs[j][i].adj=G->arcs[i][j].adj;
q1=(VertexType *)malloc(sizeof(VertexType));/*为q1和q2各分配空间*/
q2=(VertexType *)malloc(sizeof(VertexType));/*不能是q1=q2=malloc这样只分配一个空间*/
strcpy(q1->data,G->vexs[i].data);
strcpy(q2->data,G->vexs[j].data);
q1->next=q2->next=NULL;/*以便连接在每个顶点后*/
剩余5页未读,继续阅读
资源评论
lin39320028
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功