-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS_pointer.c
66 lines (64 loc) · 892 Bytes
/
BFS_pointer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//BFS(Depth First Search)-queue
#include<stdio.h>
void Insert(char Q[], int *f,int *r, char num)
{
if(*r==-1)
{
*r=0;
*f=0;
Q[*r]=num;
}
else
{
*r=*r+1;
Q[*r]=num;
}
}
char Delete(char Q[], int *f,int *r)
{
char ch=Q[*f];
if(*f==*r)
{
*f=-1;
*r=-1;
}
else
*f=*f+1;
return ch;
}
int main()
{
int n,i,j,p=1,pos=0;
char k;
int f=-1,r=-1;
printf("\n Enter the number of Vertex ");
scanf("%d",&n);
int a[n][n],status[n];
char Q[n];
for(i=0;i<n;i++)
{
printf("\n Neighbour of %c : ",i+65);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
status[i]=1;// before queue
printf(" A ");
status[0]=3;
while(p<n)
{
for(j=0;j<n;j++)
{
if(a[pos][j]==1 && status[j]==1)
{
Insert(Q,&f,&r,(char)(j+65));
status[j]=2;
}
}
k=Delete(Q,&f,&r);
printf("%c ",k);
pos=(int)k-65;
status[pos]=3;//outside queue
p++;
}
}