DFS 39
DFS 39
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
};
struct List {
};
struct Graph {
int vertices;
};
newNode->data = data;
newNode->next = NULL;
return newNode;
graph->vertices = vertices;
graph->array[i].head = NULL;
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
visited[vertex] = true;
printf("%d ", vertex);
while (currentNode) {
if (!visited[adjacentVertex]) {
currentNode = currentNode->next;
visited[i] = false;
free(visited);
while (currentNode) {
currentNode = currentNode->next;
while (currentNode) {
currentNode = currentNode->next;
free(temp);
free(graph->array);
free(graph);
// Menu-driven interface
int main() {
scanf("%d", &vertices);
do {
printf("\nMenu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &src);
scanf("%d", &dest);
if (src >= 0 && src < vertices && dest >= 0 && dest < vertices) {
} else {
break;
case 2:
printf("Enter the starting vertex for DFS (0 to %d): ", vertices - 1);
scanf("%d", &src);
DFSTraversal(graph, src);
printf("\n");
} else {
break;
case 3:
displayGraph(graph);
break;
case 4:
break;
default:
freeGraph(graph);
return 0;