Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2349905
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:07:39

#include
#include
using namespace std;
typedef enum{ERROR=0,OK=1}Status;
#define VertexNum 5
typedef int AdjType;
typedef struct graph
{
AdjType vertex[VertexNum];
int adjMatrix[VertexNum][VertexNum];
}Graph;
AdjType vertex1[VertexNum]={1,2,3,4,5};
int adjMatrix1[VertexNum][VertexNum]=
{
0,1,1,0,0,
1,0,1,1,0,
1,1,0,1,1,
0,1,1,0,1,
0,0,1,1,0,
};
Status GetVertex(Graph *graph,int pos,AdjType *vertex);
int GetPos(Graph *graph,AdjType vertex);
Status FirstAdj(Graph *graph,AdjType vertex, AdjType *nextAdj);
Status NextAdj(Graph *graph,AdjType vertex,AdjType adj, AdjType *nextAdj);
int Getpos(Graph *graph,AdjType vertex);
Status GetVertex(Graph *graph ,int pos, AdjType *vertex);
Status FirstAdj(Graph *graph,AdjType vertex, AdjType *firstAdj);
int main()
{
int i,j;
int pos;
Graph graph;
AdjType vertex;
AdjType firstAdj;
Status status;
clrscr();
for(i=0;i {
graph.vertex[i]=vertex1[i];
}
for(i=0;i {
for(j=0;j graph.adjMatrix[i][j]=adjMatrix1[i][j];
}
for(i=0;i {
GetVertex(&graph,i,&vertex);
cout< status=FirstAdj(&graph,vertex,&firstAdj);
cout<<"Adjecent node is :"< while(status!=ERROR)
{
cout< status=NextAdj(&graph,vertex,firstAdj,&nextAdj);
firstAdj;
}
}
return 0;
}
int Getpos(Graph *graph,AdjType vertex)
{
int i;
for(i=0;i {
if (graph->vertex[i]==vertex)
return i;
}
return -1;
}
Status GetVertex(Graph *graph ,int pos, AdjType *vertex)
{
if(pos>=VertexNum)
return ERROR;
*vertex=graph->vertex[pos];
return OK;
}
Status FirstAdj(Graph *graph,AdjType vertex, AdjType *firstAdj)
{
int i;
int pos;
pos=GetPos(graph,verTex);
if(pos==-1)
return ERROR;
for(i=0;i {
if (graph->adjMatrix[pos][i]==1)
{
GetVertex(graph,i,firstAdj);
return OK;
}
}
return ERROR;
}


--------------------next---------------------

阅读(1121) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~