图的遍历操作c++6.0

2025-05-08 22:43:29
推荐回答(3个)
回答1:

// 图的遍历.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include
#include "malloc.h"
#define m 5
#define NULL 0

typedef struct node
{ int adjvex;
struct node *next;
}JD;

typedef struct tnode
{
int vexdata;
JD *firstarc;
}TD;

typedef struct
{
TD ag[m];
int n;
}AD;

void DFS(AD *G,int i,char visited[]);
void DFSTraverse(AD *G);

AD *creat()
{
AD *G;
int i,m1,j;
JD *p,*p1;
printf("please input the number of graph\n");
G=(AD *)malloc(sizeof(AD));
scanf("%d",&G->n);
for(i=0;in;i++)
{
printf("please input the info of node %d\n",i);
scanf("%d",&G->ag[i].vexdata);
printf("please input the number of arcs which adj to %d\n",i);
scanf("%d",&m1);
if(m1>1)
{
printf("please input the adjvex position of the first arc\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
G->ag[i].firstarc=p;
p1=p;
}
for(j=2 ;j<=m1;j++)
{
printf("please input the position of the next arc vexdata\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
p1->next=p;
p1=p;
}
}
return G;
}

void DFSTraverse(AD *G)
{
int i;
static char visited[m];
printf("Hello World!\n");
for(i=0;in;i++)
visited[i]=0;
for(i=0;in;i++)
if(!visited[i])
DFS(G,i,visited);
}

void DFS(AD *G,int i,char visited[])
{

JD *p;
visited[i]=1;
printf("visit vertex:%d\n",G->ag[i].vexdata);
p=G->ag[i].firstarc;
while(p){
if (!visited[p->adjvex])
DFS(G,p->adjvex,visited);
p=p->next;
}
}

int main(int argc, char* argv[])
{
AD *G;
//G=NULL;
//int i;
G=creat();
printf("%d",G->n);
DFSTraverse(G);
printf("Hello World!\n");
return 0;
}

回答2:

二B 分给我吧 以后我回答 分就给你

回答3:

111