概述
#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
//表结点
typedef struct ArcNode{
intnum;
intdata;
structArcNode *nextarc;
}ArcNode;
//头结点
typedef struct VNode{
intnum;
intdata;
ArcNode *firstarc;
}VNode;
//用来纪录的空间
typedef struct{
ints;
}Simbol;
//全局变量j
int j=0;
//创建图(图示给出)
int Create_Graph(VNode *V)
{
ArcNode *p,*q;
printf("请输入个数据(依次赋给结点V1~V9):n");
inti;
for(i=1;i<=9;i++)
scanf("%d",&V[i].data);
for(i=1;i<=9;i++)
V[i].num=i;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[1].firstarc=p;
p->num=2;
p->data=V[2].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=3;
q->data=V[3].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[2].firstarc=p;
p->num=4;
p->data=V[4].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=5;
q->data=V[5].data;
p=(ArcNode *)malloc(sizeof(ArcNode));
q->nextarc=p;
p->num=1;
p->data=V[1].data;
p->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[3].firstarc=p;
p->num=6;
p->data=V[6].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=7;
q->data=V[7].data;
p=(ArcNode *)malloc(sizeof(ArcNode));
q->nextarc=p;
p->num=8;
p->data=V[8].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=1;
q->data=V[1].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[4].firstarc=p;
p->num=2;
p->data=V[2].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=9;
q->data=V[9].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[5].firstarc=p;
p->num=2;
p->data=V[2].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=9;
q->data=V[9].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[6].firstarc=p;
p->num=3;
p->data=V[3].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=7;
q->data=V[7].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[7].firstarc=p;
p->num=3;
p->data=V[3].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=6;
q->data=V[6].data;
q->nextarc=NULL;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[8].firstarc=p;
p->num=3;
p->data=V[3].data;
p->nextarc=q;
p=(ArcNode *)malloc(sizeof(ArcNode));
V[9].firstarc=p;
p->num=4;
p->data=V[4].data;
q=(ArcNode *)malloc(sizeof(ArcNode));
p->nextarc=q;
q->num=5;
q->data=V[5].data;
q->nextarc=NULL;
printf("图创建成功!!!n");
returnOK;
}
//输出图(邻接表)
int Print_Graph(VNode *V)
{
printf(" 此图的邻接表为: n");
printf(" 头结点 表结点 表结点 表结点 表结点 n");
printf(" num data num data num data num data num data n");
printf(" %d %d %d %d %d %d n",V[1].num,V[1].data,V[1].firstarc->num,V[1].firstarc->data,V[1].firstarc->nextarc->num,V[1].firstarc->nextarc->data);
printf(" %d %d %d %d %d %d %d %d n",V[2].num,V[2].data,V[2].firstarc->num,V[2].firstarc->data,V[2].firstarc->nextarc->num,V[2].firstarc->nextarc->data,V[2].firstarc->nextarc->nextarc->num,V[2].firstarc->nextarc->nextarc->data);
printf(" %d %d %d %d %d %d %d %d %d %d n",V[3].num,V[3].data,V[3].firstarc->num,V[3].firstarc->data,V[3].firstarc->nextarc->num,V[3].firstarc->nextarc->data,V[3].firstarc->nextarc->nextarc->num,V[3].firstarc->nextarc->nextarc->data,V[3].firstarc->nextarc->nextarc->nextarc->num,V[3].firstarc->nextarc->nextarc->nextarc->data);
printf(" %d %d %d %d %d %d n",V[4].num,V[4].data,V[4].firstarc->num,V[4].firstarc->data,V[4].firstarc->nextarc->num,V[4].firstarc->nextarc->data);
printf(" %d %d %d %d %d %d n",V[5].num,V[5].data,V[5].firstarc->num,V[5].firstarc->data,V[5].firstarc->nextarc->num,V[5].firstarc->nextarc->data);
printf(" %d %d %d %d %d %d n",V[6].num,V[6].data,V[6].firstarc->num,V[6].firstarc->data,V[6].firstarc->nextarc->num,V[6].firstarc->nextarc->data);
printf(" %d %d %d %d %d %d n",V[7].num,V[7].data,V[7].firstarc->num,V[7].firstarc->data,V[7].firstarc->nextarc->num,V[7].firstarc->nextarc->data);
printf(" %d %d %d %d n",V[8].num,V[8].data,V[8].firstarc->num,V[8].firstarc->data);
printf(" %d %d %d %d %d %d n",V[9].num,V[9].data,V[9].firstarc->num,V[9].firstarc->data,V[9].firstarc->nextarc->num,V[9].firstarc->nextarc->data);
returnOK;
}
//Visit函数
int Visit(VNode *V,Simbol *S,inti){
S[i].s=1;
returnV[i].data;
}
//图的遍历(深度优先)
int Traversint_Graph(VNode *V,Simbol *S)
{
ArcNode *p;
staticint i=1;
printf("%d ",Visit(V,S,i));
p=V[i].firstarc;
i=p->num;
while(p!=NULL)
{
i=p->num;
if(S[i].s!=1)Traversint_Graph(V,S);
p=p->nextarc;
}
returnOK;
}
//Fun函数
int Fun(VNode *V,Simbol *S,inti){
S[i].s=1;
returnV[i].data;
}
//图结点的查找
int Seek_Jiedian(VNode *V,Simbol *S,inta)
{
ArcNode *p;
staticint i=1;
if(Fun(V,S,i)==a){printf("该数据所在的结点为:nV%dn",V[i].num);j++;}
p=V[i].firstarc;
i=p->num;
while(p!=NULL)
{
i=p->num;
if(S[i].s!=1)Seek_Jiedian(V,S,a);
p=p->nextarc;
}
returnj;
}
//图的复制
int Copy_Graph(VNode *V,VNode *T)
{
inti;
for(i=1;i<=9;i++)
{T[i].num=V[i].num;T[i].data=V[i].data;T[i].firstarc=V[i].firstarc;}
printf(" 图复制成功!!!n");
returnOK;
}
//Fun1函数
int Fun1(VNode *V,Simbol *S,inti){
S[i].s=1;
returni;
}
//图的查询
int Seek_Information(VNode *V,Simbol *S,inta)
{
ArcNode *p;
staticint i=1;
if(Fun1(V,S,i)==a)printf("结点V%d的数据信息为:n%d",i,V[i].data);
p=V[i].firstarc;
i=p->num;
while(p!=NULL)
{
i=p->num;
if(S[i].s!=1)Seek_Information(V,S,a);
p=p->nextarc;
}
returnOK;
}
//主函数
void main()
{
VNode *V;
V=(VNode *)malloc(10*sizeof(VNode));
VNode *T;
T=(VNode *)malloc(10*sizeof(VNode));
Simbol *S;
S=(Simbol *)malloc(10*sizeof(Simbol));
intflag=1,select;
printf(" 图示如下 nn");
printf(" V1 n");
printf(" # # n");
printf(" # # n");
printf(" # # n");
printf(" V2 V3 # # # V8 n");
printf(" # # # # n");
printf(" # # # # n");
printf(" # # # # n");
printf(" V4 V5 V6 # # # # V7 n");
printf(" # # n");
printf(" # # n");
printf(" # # n");
printf(" V9 n");
printf(" nn");
printf("====================================菜单===================================n");
printf("= 1.创建图(图示给出) =n");
printf("= 2.输出图(邻接表) =n");
printf("= 3.图的遍历(深度优先) =n");
printf("= 4.图结点的查找 =n");
printf("= 5.图的复制 =n");
printf("= 6.图的查询 =n");
printf("= 7.退出操作 =n");
printf("================================支持乱序选择===============================n");
while(flag)
{
printf("n请选择菜单中的操作选项(必须先选择""创建图!创建之后再选择其他操作选项):n");
scanf("%d",&select);
switch(select)
{
case1:
Create_Graph(V);break;
case2:
Print_Graph(V);break;
case3:
Traversint_Graph(V,S);
for(select=1;select<=9;select++)
S[select].s=0;
break;
case4:
printf("请输入要查找的信息:n");
scanf("%d",&select);
if(Seek_Jiedian(V,S,select)==0)printf("此图中没有此信息!!!n");
j=0;
for(select=1;select<=9;select++)
S[select].s=0;
break;
case5:
Copy_Graph(V,T);
Print_Graph(T);break;
case6:
printf("请输入要查找的结点(~9):n");
scanf("%d",&select);
if(select>=1&&select<=9)Seek_Information(V,S,select);
elseprintf("您输入的数据有误!!!n");
for(select=1;select<=9;select++)
S[select].s=0;
break;
case7:
flag=0;break;
default:
printf("您输入的数据有误!!!请重新输入!!!n");
}
}
}
最后
以上就是机灵发夹为你收集整理的【数据结构】图的基本操作的全部内容,希望文章能够帮你解决【数据结构】图的基本操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复