最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
图的广度遍历
时间:2008-04-26 编辑:简简单单 来源:一聚教程网
#include
#include
#include
#include
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 QNode{
int elem;
struct QNode *next;
}QNode,*QueuePtr;/*队数据类型*/
typedef struct
{ QueuePtr front;/*队头指针*/
QueuePtr rear;/*队尾指针*/
}LinkQueue;
int InitQueue(LinkQueue *Q)/*初始化队列*/
{ Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));/*分配空间*/
if(!Q->front) exit(0);
(Q->front)->next=NULL;
return 1;
}
int EnQueue(LinkQueue *Q,int e)/*插在队最后*/
{QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));/*分配空间*/
if(!p) exit(0);
p->elem=e;p->next=NULL;
(Q->rear)->next=p;
Q->rear=p;/*重新设置队尾*/
return 1;
}
int QueueEmpty(LinkQueue Q)/*队是否为空*/
{ if(Q.front==Q.rear) return 1;
else return 0;}
int DelQueue(LinkQueue *Q,int *e)/*删除队的第一个元素*/
{QueuePtr p;
if(QueueEmpty(*Q)) return 0;
p=(Q->front)->next;
*e=p->elem;
(Q->front)->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return 1;
}
/* ************************ */
void InitGraph(MGraph *G)/*初始图*/
{ int i,nu,mu;
printf("输入顶点的个数和(边)弧的个数:");
scanf("%d%d",&nu,&mu);
G->arcs=(ArcCell **)malloc(nu*sizeof(ArcCell *));
for(i=0;i
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);
相关文章
- java 完全二叉树的构建与四种遍历方法示例 05-27
- 详解Java中list,set,map的遍历与增强for循环 03-25
- 详解python中字典的循环遍历的两种方式 03-17
- 二叉搜索树与树的遍历非递归练习 08-13
- java中遍历List的几种方法 02-25
- java中遍历Map的常用方法总结 01-05
热门栏目
-
php教程
php入门 php安全 php安装 php常用代码 php高级应用 -
asp.net教程
基础入门 .Net开发 C语言 VB.Net语言 WebService -
手机开发
安卓教程 ios7教程 Windows Phone Windows Mobile 手机常见问题 -
css教程
CSS入门 常用代码 经典案例 样式布局 高级应用 -
网页制作
设计基础 Dreamweaver Frontpage js教程 XNL/XSLT -
办公数码
word excel powerpoint 金山WPS 电脑新手 -
jsp教程
Application与Applet J2EE/EJB/服务器 J2ME开发 Java基础 Java技巧及代码