# Untitled

unknown
plain_text
7 months ago
2.3 kB
2
Indexable
Never
```#include <stdio.h>
#include <stdlib.h>

// Node structure for adjacency list
struct Node {
int vertex;
struct Node* next;
};

};

// Graph structure
struct Graph {
int vertices;
};

// Function to create a new node
struct Node* createNode(int vertex) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}

// Function to create a graph with a given number of vertices
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->vertices = vertices;

// Create an array of adjacency lists

// Initialize each adjacency list as empty
for (int i = 0; i < vertices; ++i) {
}

return graph;
}

// Function to add an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add an edge from src to dest
struct Node* newNode = createNode(dest);

// Since the graph is undirected, add an edge from dest to src as well
newNode = createNode(src);
}

// Function to print the adjacency list representation of the graph
void printGraph(struct Graph* graph) {
for (int i = 0; i < graph->vertices; ++i) {
printf("Adjacency list of vertex %d\n", i);
while (temp) {
printf(" -> %d", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

// Driver program to test the graph functions
int main() {
// Create a graph with 5 vertices
struct Graph* graph = createGraph(5);