当前位置 : 主页 > 手机开发 > harmonyos >

邻接表 DFS

来源:互联网 收集:自由互联 发布时间:2023-08-25
#includestdio.h #includestdlib.h struct ele { int num; int value; ele *next; }p[125]; bool b[125]; void DFS(int i,int n) { int j,k; ele *q; printf("%d ",i);//1 b[i]=true; //2 q=p[i]; while(q) { if(b[i]==false) DFS(q-num,n);//1 q=q-next; } }
#include<stdio.h> 

 #include<stdlib.h> 

 struct ele 

 { 

int num; 

int value; 

ele *next; 
  

 }p[125]; 

 bool b[125]; 

 void DFS(int i,int n) 

 { 

int j,k; 

ele *q; 

printf("%d ",i);//1 

b[i]=true; 
  //2 

q=&p[i]; 

while(q) 

{ 

if(b[i]==false) 

DFS(q->num,n);//1 

q=q->next; 

} 

 } 

 int main() 

 { 

int n,m; 

ele *t; 

scanf("%d%d",&n,&m); 

int i,j,k; 

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

{ 

p[i].num=i; 

p[i].value=-1; 

p[i].next=NULL; 

} 

while(m--) 

{ 

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

t=(ele*)malloc(sizeof(ele));//1 

t->num=j; 
  //2 

t->value=1; 
  //3 

t->next=p[i].next; 
 //4 

p[i].next=t; 
  //5 

t=(ele*)malloc(sizeof(ele)); 

t->num=i; 
  //1 

t->value=1; 
  //2 

t->next=p[j].next; 
 //3 

p[j].next=t; 
  //4 

} 

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

if(b[i]==false) 

DFS(i,n); 

return 0; 

 }
上一篇:STL vector
下一篇:没有了
网友评论