-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet02.c
More file actions
112 lines (91 loc) · 2.75 KB
/
Set02.c
File metadata and controls
112 lines (91 loc) · 2.75 KB
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
104
105
106
107
108
109
110
111
112
/* [Practical Exam - 2025 : Set 02]
Write a program to take an undirected graph as input from the user and store it using
adjacency list (linked list) representations and print it. Then take two nodes X and Y as inputs
from user and check whether there is an edge between node X and Y.
Graph: 2-----3----4
\ /
\ /
1
Test Cases: Enter two nodes: 1 and 3 → Output: Yes, there is an edge between node l and 3.
Enter two nodes: 1 and 4 → Output: No, there is no edge between node 1 and 4.
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Graph {
int V;
struct Node** adjList;
};
// Create new adjacency node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Create graph
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->adjList = (struct Node**)malloc(V * sizeof(struct Node*));
for (int i = 0; i < V; i++)
graph->adjList[i] = NULL;
return graph;
}
// Add edge (undirected)
void addEdge(struct Graph* graph, int src, int dest) {
struct Node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjList[dest];
graph->adjList[dest] = newNode;
}
// Print adjacency list
void printGraph(struct Graph* graph) {
for (int i = 0; i < graph->V; i++) {
printf("%d: ", i);
struct Node* temp = graph->adjList[i];
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
// Check edge between X and Y
void checkEdge(struct Graph* graph, int x, int y) {
struct Node* temp = graph->adjList[x];
while (temp != NULL) {
if (temp->data == y) {
printf("Yes, there is an edge between node %d and %d.\n", x, y);
return;
}
temp = temp->next;
}
printf("No, there is no edge between node %d and %d.\n", x, y);
}
int main() {
int V, E;
printf("Enter number of vertices: ");
scanf("%d", &V);
struct Graph* graph = createGraph(V);
printf("Enter number of edges: ");
scanf("%d", &E);
printf("Enter edges (u v):\n");
for (int i = 0; i < E; i++) {
int u, v;
scanf("%d %d", &u, &v);
addEdge(graph, u, v);
}
printf("\nAdjacency List Representation:\n");
printGraph(graph);
int x, y;
printf("\nEnter two nodes to check edge: ");
scanf("%d %d", &x, &y);
checkEdge(graph, x, y);
return 0;
}