邻接矩阵、邻接表表示图时的深度优先序列、广度优先序列

2024-12-29 23:50:14
推荐回答(1个)
回答1:

#include

#include

#include

#include

#define maxsize 64

#define TRUE 1

#define FALSE 0

#define n 10

#define e 13

typedef char datatype ;

typedef char vextype;
typedef int adjtype;

typedef struct

{

vextype vexs[maxsize];

adjtype arcs[maxsize][maxsize];
}graph;

typedef struct

{

datatype data[maxsize];

int front,rear;
}sequeue;

typedef struct node

{

int adjvex;

struct node *next;

}edgenode;

typedef struct

{

vextype vertex;

edgenode *link;
}vexnode;

vexnode gl[maxsize];

graph *ga;
sequeue *Q;

graph *CREATGRAPH()

{

int i,j,k;

char ch;

system("cls");

scanf("%c",&ch);

printf("\n请输入顶点信息(邻接矩阵): ");

for(i=1;i<=n;i++)

scanf("%c",&ga->vexs[i]);

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

ga->arcs[i][j]=0;

printf("\n输入节点信息与权值:\n");

for(k=0;k
{

scanf("%d%d",&i,&j);//读入一条变得两端顶点序号i,j及边上的权值

ga->arcs[i][j]=1;

ga->arcs[j][i]=1;

}

return ga;
}

void PRINT()

{

int i,j;

system("cls");

printf("\n对应的邻接矩阵是:\n\n");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

printf("%5d",ga->arcs[i][j]);

printf("\n");

}
}

void CREATADJLIST()

{

int i,j,k;

edgenode *s;

char ch;

system("cls");

printf("请输入顶点信息: ");

scanf("%c",&ch);

for(i=1;i<=n;i++)

{

gl[i].vertex=getchar();

gl[i].link=NULL;

}

printf("输入边表节点信息:\n");

for(k=1;k<=e;k++)

{

scanf("%d %d",&i,&j);

s=(edgenode *)malloc(sizeof(edgenode));

s->adjvex=j;

s->next=gl[i].link;

gl[i].link=s;

s=(edgenode *)malloc(sizeof(edgenode));

s->adjvex=i;

s->next=gl[j].link;

gl[j].link=s;

}
}

void PRINTL()

{

int i;

edgenode *s;

system("cls");

printf("\n对应的邻接表是:\n");

for(i=1;i<=n;i++)

{

s=gl[i].link;
printf("%3c",gl[i].vertex);

while(s!=NULL)

{

printf("%5d",s->adjvex);

s=s->next;

}

printf("\n");

}
}

sequeue *SETNULL(sequeue *P)

{

P->front=maxsize-1;

P->rear=maxsize-1;

return P;
}

int EMPTY(sequeue *Q)

{

if(Q->rear==Q->front)

return TRUE;

else

return FALSE;
}

sequeue *ENQUEUE(sequeue *Q,int k)

{

if(Q->front==(Q->rear+1)%maxsize)

{

printf("队列已满!\n");

return NULL;

}

else

{

Q->rear=(Q->rear+1)%maxsize;

Q->data[Q->rear]=k;

}

return Q;

}

int DEQUEUE(sequeue *Q)

{

if(EMPTY(Q))

{

printf("队列为空,无法出队!\n");

return FALSE;

}

else

{

Q->front=(Q->front+1)%maxsize;

return Q->data[Q->front];

}

return 1;

}

void BFS(int k)

{

int i,j;

int visited[maxsize]={0};

printf("\n广度优先遍历后得到的序列是: ");

Q=SETNULL(Q);

printf("%3c",ga->vexs[k]);

visited[k]=TRUE;

Q=ENQUEUE(Q,k);

while(!EMPTY(Q))

{

i=DEQUEUE(Q);

for(j=1;j<=n;j++)

if((ga->arcs[i][j]==1)&&(!visited[j]))

{

printf("%3c",ga->vexs[j]);

visited[j]=TRUE;

Q=ENQUEUE(Q,j);

}

}

printf("\n\n深度优先遍历后得到的序列是: ");

}

void BFSL(int k)

{

int i;

int visited[maxsize]={0};

edgenode *p;

Q=SETNULL(Q);

printf("\n广度优先遍历后得到的序列是: ");

printf("%3c",gl[k].vertex);

visited[k]=TRUE;

Q=ENQUEUE(Q,k);

while(!EMPTY(Q))

{

i=DEQUEUE(Q);

p=gl[i].link;

while(p!=NULL)

{

if(!visited[p->adjvex])

{

printf("%3c",gl[p->adjvex].vertex);

visited[p->adjvex]=TRUE;

Q=ENQUEUE(Q,p->adjvex);

}

p=p->next;

}

}

printf("\n\n深度优先遍历后得到的序列是: ");
}

void DFS(int i)

{

int j;

static int visited[maxsize]={0};

printf("%3c",ga->vexs[i]);

visited[i]=TRUE;

for(j=1;j<=n;j++)

if((ga->arcs[i][j]==1)&&(!visited[j]))

DFS (j);

}

void DFSL(int k)

{

int j;

edgenode *p;

static int visited[maxsize]={0};

printf("%3c",gl[k].vertex);

visited[k]=TRUE;

p=gl[k].link;

while(p)

{

j=p->adjvex;

if(!visited[j])

DFSL(j);

p=p->next;

}

}

void main()

{

int i,k;

int ch;

Q=malloc(sizeof(graph));

ga=malloc(sizeof(graph));

while(1)

{

printf("\n*********************************************\n\n");

printf("\t1.以邻接表遍历连通图\n");

printf("\t2.以邻接矩阵遍历连通图\n");

printf("\t3.退出程序\n");

printf("\n\n*********************************************\n\n");

printf("输入你的选择: ");

scanf("%d",&ch);

switch(ch)

{

case 1: CREATADJLIST();

PRINTL();

printf("想从第几号结点开始: ");

scanf("%d",&k);

BFSL(k);

DFSL(k);break;

case 2: ga=CREATGRAPH();

PRINT();

printf("想从第几号结点开始: ");

scanf("%d",&i);

BFS(i);

DFS(i);break;

case 3:exit (0);

}

}

}