#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define
#define
//表结点
typedef struct
intnum;
intdata;
structArcNode *nextarc;
}ArcNode;
//头结点
typedef struct
intnum;
intdata;
ArcNode *firstarc;
}VNode;
//用来纪录的空间
typedef struct{
ints;
}Simbol;
//全局变量j
int
//创建图(图示给出)
int
{
ArcNode *p,*q;
"请输入个数据(依次赋给结点V1~V9):\n");
inti;
for(i=1;i<=9;i++)
"%d",&V[i].data);
for(i=1;i<=9;i++)
V[i].num=i;
sizeof(ArcNode));
V[1].firstarc=p;
p->num=2;
p->data=V[2].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=3;
q->data=V[3].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[2].firstarc=p;
p->num=4;
p->data=V[4].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=5;
q->data=V[5].data;
sizeof(ArcNode));
q->nextarc=p;
p->num=1;
p->data=V[1].data;
p->nextarc=NULL;
sizeof(ArcNode));
V[3].firstarc=p;
p->num=6;
p->data=V[6].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=7;
q->data=V[7].data;
sizeof(ArcNode));
q->nextarc=p;
p->num=8;
p->data=V[8].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=1;
q->data=V[1].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[4].firstarc=p;
p->num=2;
p->data=V[2].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=9;
q->data=V[9].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[5].firstarc=p;
p->num=2;
p->data=V[2].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=9;
q->data=V[9].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[6].firstarc=p;
p->num=3;
p->data=V[3].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=7;
q->data=V[7].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[7].firstarc=p;
p->num=3;
p->data=V[3].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=6;
q->data=V[6].data;
q->nextarc=NULL;
sizeof(ArcNode));
V[8].firstarc=p;
p->num=3;
p->data=V[3].data;
p->nextarc=q;
sizeof(ArcNode));
V[9].firstarc=p;
p->num=4;
p->data=V[4].data;
sizeof(ArcNode));
p->nextarc=q;
q->num=5;
q->data=V[5].data;
q->nextarc=NULL;
"图创建成功!!!\n");
returnOK;
}
//输出图(邻接表)
int
{
" 此图的邻接表为: \n");
" 头结点 表结点 表结点 表结点 表结点 \n");
" num data num data num data num data num data \n");
" %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);
" %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);
" %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);
" %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);
" %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);
" %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);
" %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);
" %d %d %d %d \n",V[8].num,V[8].data,V[8].firstarc->num,V[8].firstarc->data);
" %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
{
ArcNode *p;
staticint
"%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
if(Fun(V,S,i)==a){printf("该数据所在的结点为:\nV%d\n",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
{
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;}
" 图复制成功!!!\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
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
{
VNode *V;
sizeof(VNode));
VNode *T;
sizeof(VNode));
Simbol *S;
sizeof(Simbol));
intflag=1,select;
" 图示如下 \n\n");
" V1 \n");
" # # \n");
" # # \n");
" # # \n");
" V2 V3 # # # V8 \n");
" # # # # \n");
" # # # # \n");
" # # # # \n");
" V4 V5 V6 # # # # V7 \n");
" # # \n");
" # # \n");
" # # \n");
" V9 \n");
" \n\n");
"====================================菜单===================================\n");
"= 1.创建图(图示给出) =\n");
"= 2.输出图(邻接表) =\n");
"= 3.图的遍历(深度优先) =\n");
"= 4.图结点的查找 =\n");
"= 5.图的复制 =\n");
"= 6.图的查询 =\n");
"= 7.退出操作 =\n");
"================================支持乱序选择===============================\n");
while(flag)
{
"\n请选择菜单中的操作选项(必须先选择""创建图!创建之后再选择其他操作选项):\n");
"%d",&select);
switch(select)
{
case1:
break;
case2:
break;
case3:
Traversint_Graph(V,S);
for(select=1;select<=9;select++)
S[select].s=0;
break;
case4:
"请输入要查找的信息:\n");
"%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);
break;
case6:
"请输入要查找的结点(~9):\n");
"%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:
break;
default:
"您输入的数据有误!!!请重新输入!!!\n");
}
}
}
【数据结构】图的基本操作
原创
©著作权归作者所有:来自51CTO博客作者RandTsui的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:【数据结构】栈的基本操作
下一篇:【数据结构】顺序表的基本操作
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【数据结构】详细解读串的基本概念与基本操作
【数据结构】第四章——串详细介绍串的基本概念和基本操作……
数据结构 C语言 字符串 ASCII码值 转义字符 -
【数据结构】串的基本操作原来可以这样实现……
【数据结构】第四章——串详细介绍C语言实现串的基本操作……
数据结构 主串 子串 串定位 C语言 -
C++数据结构实验--图的基本操作
x
1024程序员节 i++ ci #include -
数据结构:栈的基本操作
int main(){ SeqStac...
出栈 堆栈 #define 入栈 初始化