Kindly answer the following C programming question based on the following code! #include #include #define MAX_VERTEX_NUM 20 //MAX number of vertices typedef struct edgenode { char vertex; int weight; struct edgenode *nextEdge; } EdgeNode, *EdgeNodePtr; typedef struct vertexnode { char vertex; EdgeNodePtr firstEdge; } VertexNode, *VertexNodePtr; typedef struct { int size; char vertices[MAX_VERTEX_NUM]; VertexNode AdjList[MAX_VERTEX_NUM]; } AdjListGraph, *Graph; //Create a graph of n vertices, use dynamic memory //- given the vertices and the size, return the graph Graph createGraph(char vertices[], int size) { Graph graph = (Graph)malloc(sizeof(AdjListGraph)); graph->size = size; for (int i = 0; i < size; i++) { graph->vertices[i] = vertices[i]; graph->AdjList[i].vertex = vertices[i]; graph->AdjList[i].firstEdge = NULL; } return graph; } //Print the graph as an adjacency matrix void printGraph(Graph graph) { if (graph == NULL) { puts("Graph is empty"); return; } for (int i = 0; i < graph->size; i++) { printf("|%c|->", graph->vertices[i]); EdgeNodePtr curr = graph->AdjList[i].firstEdge; while(curr != NULL) { printf("|%c|%d|->", curr->vertex, curr->weight); curr = curr->nextEdge; } puts("NULL"); } } //Delete the graph, i.e. free the memory Graph deleteGraph(Graph graph) { if (graph == NULL) { puts("Graph is empty"); return NULL; } for (int i = 0; i < graph->size; i++) { EdgeNodePtr pre = NULL; EdgeNodePtr curr = graph->AdjList[i].firstEdge; while(curr != NULL) { pre = curr; curr = curr->nextEdge; free(pre); } } free(graph); return NULL; } //Do NOT MODIFY ANY CODE ABOVE THIS LINE -------------------------------------- void addEdge(Graph graph, int u, int v, int w) { //TODO } //Do NOT MODIFY ANY CODE BELOW THIS LINE -------------------------------------- int main() { //Create the graph with 5 vertices char vertices[MAX_VERTEX_NUM]; for (int i = 0; i < MAX_VERTEX_NUM; i++) vertices[i] = 'A' + i; Graph g1 = createGraph(vertices, 5); //Add edges int edges[] = {0,1,3, 1,2,2, 1,3,4, 2,4,5, 3,2,5, 4,3,2}; for (int i = 0; i < (int)(sizeof(edges)/sizeof(int)); i += 3) addEdge(g1, edges[i], edges[i+1], edges[i+2]); printGraph(g1); //Delete the graph g1 = deleteGraph(g1); } //end of main
Kindly answer the following C programming question based on the following code!
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20 //MAX number of vertices
typedef struct edgenode {
char vertex;
int weight;
struct edgenode *nextEdge;
} EdgeNode, *EdgeNodePtr;
typedef struct vertexnode {
char vertex;
EdgeNodePtr firstEdge;
} VertexNode, *VertexNodePtr;
typedef struct {
int size;
char vertices[MAX_VERTEX_NUM];
VertexNode AdjList[MAX_VERTEX_NUM];
} AdjListGraph, *Graph;
//Create a graph of n vertices, use dynamic memory
//- given the vertices and the size, return the graph
Graph createGraph(char vertices[], int size) {
Graph graph = (Graph)malloc(sizeof(AdjListGraph));
graph->size = size;
for (int i = 0; i < size; i++) {
graph->vertices[i] = vertices[i];
graph->AdjList[i].vertex = vertices[i];
graph->AdjList[i].firstEdge = NULL;
}
return graph;
}
//Print the graph as an adjacency matrix
void printGraph(Graph graph) {
if (graph == NULL) {
puts("Graph is empty");
return;
}
for (int i = 0; i < graph->size; i++) {
printf("|%c|->", graph->vertices[i]);
EdgeNodePtr curr = graph->AdjList[i].firstEdge;
while(curr != NULL) {
printf("|%c|%d|->", curr->vertex, curr->weight);
curr = curr->nextEdge;
}
puts("NULL");
}
}
//Delete the graph, i.e. free the memory
Graph deleteGraph(Graph graph) {
if (graph == NULL) {
puts("Graph is empty");
return NULL;
}
for (int i = 0; i < graph->size; i++) {
EdgeNodePtr pre = NULL;
EdgeNodePtr curr = graph->AdjList[i].firstEdge;
while(curr != NULL) {
pre = curr;
curr = curr->nextEdge;
free(pre);
}
}
free(graph);
return NULL;
}
//Do NOT MODIFY ANY CODE ABOVE THIS LINE --------------------------------------
void addEdge(Graph graph, int u, int v, int w) {
//TODO
}
//Do NOT MODIFY ANY CODE BELOW THIS LINE --------------------------------------
int main() {
//Create the graph with 5 vertices
char vertices[MAX_VERTEX_NUM];
for (int i = 0; i < MAX_VERTEX_NUM; i++)
vertices[i] = 'A' + i;
Graph g1 = createGraph(vertices, 5);
//Add edges
int edges[] = {0,1,3, 1,2,2, 1,3,4, 2,4,5, 3,2,5, 4,3,2};
for (int i = 0; i < (int)(sizeof(edges)/sizeof(int)); i += 3)
addEdge(g1, edges[i], edges[i+1], edges[i+2]);
printGraph(g1);
//Delete the graph
g1 = deleteGraph(g1);
} //end of main


Step by step
Solved in 4 steps with 3 images









