话不多说,直接上代码。
根据给定的顶点为A,B,C,D,E,F,G,H,I的做组成图的邻接矩阵,给出图的深度优先及广度优先遍历序列。
深度优先,我用递归做的。广度优先用队列。
#include <stdio.h>
#include <stdlib.h>
//根据给定的顶点为A,B,C,D,E,F,G,H,I的做组成图的邻接矩阵,给出图的深度优先及广度优先遍历序列
//节点类
typedef struct node{
char data;
struct node *next;
}node;
typedef struct queue
{
node *data[20];
int front;
int rear;
}queue;
//队列初始化
queue *initq(queue*q)
{
int i=0;
for ( i = 0; i < 20; i++)
{
q->data[i]=calloc(1,sizeof(node));
}
q->front=q->rear=0;
return q;
}
//入队列
void enq(queue*q,node*p)
{
if((q->front+1)%20==q->rear)printf("队列已满");
q->data[q->front]=p;
q->front=(q->front+1)%20;
}
//出队列
node *deq(queue*q)
{
if (q->front==q->rear)
{
printf("队列已空");
}
node*p;
p=calloc(1,sizeof(node));
p=q->data[q->rear];
q->rear=(q->rear+1)%20;
return p;
}
//判断标志数组是否全覆盖
int isone(int c[]){
int i=0;
while(i<9){
if(c[i]==1){
i++;
}else{
return -1;
}
}
if(i==9){
return 1;
}
}
//返回数组第一个不为1的地址
int where(int c[]){
int i=0;
while(c[i]==1){
i++;
if(i==9){
return 9;
}
}
return i;
}
//深度优先遍历
void hsf(node *h[],int c[],node* d){
int i=0;
node *p;
calloc(1,sizeof(node));
//每走一步,递归一次
//首次遍历
if(c[0]==0)
{
p=h[0];
c[0]=1;
printf("%c",p->data);
while(p->next!=0){
hsf(h,c,h[(int)(p->next->data-'A')]);
p=p->next;
}
}
//非首次遍历
else if (d!=NULL)
{
p=d;
if (c[(int)(p->data-'A')]==0)
{
c[(int)(p->data-'A')]=1;
printf("%c",p->data);
while(p->next!=NULL)
{
hsf(h,c,h[(int)(p->next->data-'A')]);
p=p->next;
}
if(c[(int)(p->data-'A')]==1&&p->next!=NULL)
{
p=p->next;
}
}
else if(c[(int)(p->data-'A')]==1)
{
return;
}
else
{
printf("%d",(int)(p->data-'A'));
return;
}
}
//最后访问单独的节点
else if(isone(c)==-1)
{
i=where(c);
c[i]=1;
printf("%c",h[i]->data);
p=h[i]->next;
while(p->next!=NULL)
{
p=p->next;
if(c[(int)(p->data-'A')]==0)
{
c[(int)(p->data-'A')]=1;
printf("%c",p->data);
}
else
{
break;
}
}
}
}
//广度优先遍历
void bsf(node *h[],int c[],queue*q)
{
int i=0;
node*p;
p=calloc(1,sizeof(node));
node*temp;
temp=calloc(1,sizeof(node));
if (c[0]==0)
{
//首次遍历
p=h[0];
printf("%c",p->data);
c[0]=1;
while(p->next!=NULL)
{
enq(q,h[(int)(p->next->data-'A')]);
p=p->next;
}
}
while (q->front!=q->rear)
{
p=deq(q);
temp=p;
while(temp->next!=NULL)
{
temp=temp->next;
if (c[(int)(temp->data-'A')]==0)
{
enq(q,h[(int)(temp->data-'A')]);
}
}
if (c[(int)(p->data-'A')]==1)
{
continue;
}
else
{
c[(int)(p->data-'A')]=1;
printf("%c",p->data);
continue;
}
}
}
int main()
{
char a[25]={'\0'};//接收输入
int b[9]={0};//提取a的有用信息
int i=0;
int j=0;
int k=0;
node *h[9];
node *p;
p=calloc(1,sizeof(node));
//创建头节点
for(i=0;i<9;i++){
h[i]=calloc(1,sizeof(node));
h[i]->data='A'+i;
}//创建完毕
//输入
for(i=0;i<9;i++){
gets(a);
j=0;k=0;
while(a[j]!='\0'){
//printf("%d",(int)a[j]-(int)'0');
if((int)a[j]-(int)'0'>=0&&a[j]<='1')
{
b[k]=(int)a[j]-(int)'0';
k++;j++;
}
else if (a[j]=='-')
{
b[k]=-1;
j+=2;
k++;
}
else if(a[j]==' ')
{
j++;
}
}
//提取完成
j=0;
p=h[i];
while(j<9){
if(i==j){
j++;
}else if(b[j]==1){
node *q;
q=calloc(1,sizeof(node));
p->next=q;
q->data='A'+j;
q->next=NULL;
j++;
p=p->next;
}else{
j++;
}
}
}
int c[9]={0};
hsf(h,c,NULL);
queue *q;
q=calloc(1,sizeof(queue));
q=initq(q);
for ( i = 0; i < 9; i++)
{
c[i]=0;
}
printf("\n");
bsf(h,c,q);
return 0;
}