-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp10BFS.c
79 lines (75 loc) · 1.37 KB
/
Exp10BFS.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
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
#include<stdlib.h>
int matrix[5][5]={{0,1,1,1,0},{1,0,1,0,0},{1,1,0,0,1},{1,0,0,0,0},{0,0,1,0,0}};
int visited[5] = {0,0,0,0,0};
struct node{
int data;
struct node *next;
}*head, *t, *prev, *newNode;
void initialiseHead()
{
head = NULL;
}
int check()
{
int i;
for(i=0; i<5; i++){
if(visited[i]==0)
return 1;
}
return 0;
}
void enqueue(int val)
{
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if(head==NULL){
head = newNode;
}
else{
t = head;
while(t->next!=NULL){
t = t->next;
}
t->next = newNode;
}
}
int dequeue()
{
int val = -1;
if(head==NULL){
}
else{
t = head;
head = t->next;
val = t->data;
free(t);
}
return val;
}
void bfs()
{
int i, j=0,temp, val=0;
enqueue(0);
visited[0] = 1;
while(check()==1){
j = dequeue();
printf("%d ", j);
for(i=0; i<5; i++){
if(matrix[j][i]==1 && visited[i]==0){
enqueue(i);
visited[i] = 1;
}
}
}
while(val!=-1){
val = dequeue();
if(val!=-1)
printf("%d ", val);
}
}
void main()
{
bfs();
}