#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) |