-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp10DFS.c
103 lines (99 loc) · 1.87 KB
/
Exp10DFS.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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 push(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 pop()
{
int val = -1;
if(head==NULL){
//printf("\nEmpty Linked List!\n");
}
else{
t = head;
if(t->next==NULL){
val = (t->data);
head = NULL;
free(t);
}
else{
prev = t;
t = t->next;
while(t->next!=NULL){
prev = t;
t = t->next;
}
val = (t->data);
prev->next = NULL;
free(t);
}
}
return val;
}
void display()
{
printf("\n Displaying:");
t = head;
while(t!=NULL){
printf("%d ", t->data);
t = t->next;
}
printf("\n");
}
void dfs()
{
int i, j=0,temp, val=0;
push(0);
visited[0] = 1;
while(check()==1){
j = pop();
printf("%d ", j);
for(i=0; i<5; i++){
if(matrix[j][i]==1 && visited[i]==0){
push(i);
visited[i] = 1;
}
}
}
while(val!=-1){
val = pop();
if(val!=-1)
printf("%d ", val);
}
}
void main()
{
dfs();
}